获取Google电子表格的说明

时间:2018-02-28 15:38:42

标签: google-apps-script google-sheets

我有以下代码,可通过用户输入搜索字词在Google云端硬盘上搜索和列出电子表格的各种参数。

如何更改代码以获取当前有效Google电子表格的“说明”?(新手)我似乎无法使其正常工作:仅通过搜索选项

function onOpen() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var searchMenuEntries = [ {name: "Search in all files", functionName: "search"}];
    ss.addMenu("Search Google Drive", searchMenuEntries);
}
function search() {

    // Prompt the user for a search term
    var searchTerm = Browser.inputBox("Enter the search parameters");

    // Get the active spreadsheet and the active sheet
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();

    // Search the files in the user's Google Drive for the search term
    var files = DriveApp.searchFiles("fullText contains '" + searchTerm.replace("'", "\'") + "'");

    // Create an array to store our data to be written to the sheet.
    var output = [];

    // Loop through the results and get the file name, file type, and URL.
    while (files.hasNext()) {
        var file = files.next();
        var fileId = file.getId();
        var updated = file.getLastUpdated();

        var fileType = file.getMimeType();
        if (fileType === MimeType.GOOGLE_SHEETS) {
            var doc = SpreadsheetApp.openById(fileId);
            var name = doc.getName();
            var description = file.getDescription();
            var url = doc.getUrl();

            var range = doc.getRange("A1:C11");
            var tags = range.getCell(1, 2).getValue(); 
            var email = range.getCell(11, 2).getValue();
            var mobile = range.getCell(7, 2).getValue();

            // Push the file details to our output array (essentially pushing a row of data).
            output.push([ tags, name, email, mobile, description, url, updated, fileId]);
        }
    }

    // Set up the spreadsheet to display the results.
    var headers = [["Tags", "File Name", "Email", "Mobile", "Description", "URL", "Updated", "File ID"]];
    sheet.clear();
    sheet.getRange("A1:H1").setValues(headers);

    // Write data to the original sheet.
    sheet.getRange(2, 1, output.length, 8).setValues(output);
}

感激不尽的任何帮助。提前致谢。

1 个答案:

答案 0 :(得分:1)

我建议编写一个新函数(例如writeDescription()),而不是破坏search()函数,并从onOpen添加对它的引用,以便您可以轻松调用它。 / p>

在您的新功能中,您可能不想清除活动表 - 我建议您访问特定表单,即

var sheet = ss.getSheetByName('Wb Description');
if(!sheet)
    sheet = ss.createSheet('Wb Description');
sheet.clear();

作为i-i mentions,您仍需要使用Drive API获取有关文件的所需信息:

var file = DriveApp.getFileById(ss.getId());
var description = file.getDescription();

如果您只想要描述,那么您只需要编写它:

sheet.getRange("A1:A2").setValues([["Description:"], [description]]);

然后上面的行将写出"描述"进入A1并将电子表格的描述转化为A2。