【问题标题】:Header section within Google DocsGoogle 文档中的标题部分
【发布时间】:2015-05-12 16:18:56
【问题描述】:

所以,我修改了一些代码,将文件搜索的描述字段添加到电子表格中,这对我来说可能很有用。但是,理想情况下,我想进行文件搜索,然后将 google 文档标题部分中的详细信息添加到电子表格列中。

我假设的问题是: var header= file.getHeader();

如何查询执行的搜索以返回文档标题的内容?请问有人可以帮忙吗?

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
// See documentation for search parameters you can use
// https://developers.google.com/apps-script/reference/drive/drive-app#searchFiles(String)


 var files = DriveApp.searchFiles("fullText contains  '"+searchTerm.replace("'","\'")+"'");
 var output = [];

 while (files.hasNext()) {
 var file = files.next();
 var fileId = file.getId();

 var fileType = file.getMimeType();

 if (fileType === MimeType.GOOGLE_DOCS) {
 var doc = DocumentApp.openById(fileId);

 var name = doc.getName();
 var header= doc.getHeader().getText();
 var url = doc.getUrl();

 // Set up the spreadsheet to display the results

var headers = [["File ID", "File Type", "File Name", "Header", "URL"]];
sheet.clear();
sheet.getRange("A1:E1").setValues(headers);


 // push the file details to our output array (essentially pushing a row of data)

output.push([ fileId, fileType, name, header, url]);
}


// write data to the sheet

sheet.getRange(2, 1, output.length, 5).setValues(output);
}
}

【问题讨论】:

  • 不确定这是什么,但你可以试试 file.getDescription()
  • 您需要先使用 DocumentApp 打开文件,然后从那里获取标题。 var file = files.next() 为您提供有关文档的信息,但不是它的内容。查看Document 了解更多信息。
  • 好的,谢谢大家,搜索描述选项很有用,但是对于这些预先存在的文件,我没有使用此字段,因此在文档标题或文档正文中搜索必须是最方便的方法向前走。我将尝试使用 documentapp 选项并发布代码,假设我也可以让它工作 - 毕竟我是初学者的新手。

标签: google-apps-script header google-docs document


【解决方案1】:

手动在文档中插入标题,然后打开文档并从 url 中获取 ID。然后运行一些测试代码:

function getHeader() {
  var doc = DocumentApp.openById('your ID here');
  var hdear = doc.getHeader();

  Logger.log('hdear: ' + hdear);
};

从脚本编辑器运行代码,然后在 VIEW 菜单中打开 LOGS。也可以用调试器逐行运行代码。

在日志中,应该显示“HeaderSection”


应该使用 DriveApp 来获取文件列表,但随后您需要使用 DocumentApp 来获取标题。

function myFunction() {
  var files = DriveApp.searchFiles("fullText contains '"+searchTerm.replace("'","\'")+"'");
  var output = [];

  while (files.hasNext()) {
    var file = files.next();
    var fileId = file.getId();

    var fileType = file.getMimeType();

    if (fileType === MimeType.GOOGLE_DOCS) {
      var doc = DocumentApp.openById(fileId);

      var name = doc.getName();
      var header= doc.getHeader().getText();
      var url = doc.getUrl();

      output.push(name);
      output.push(fileType);
      output.push(header);
      output.push(url);
    };
  };

  var outerArray = [];
  outerArray.push(output);
  // write data to the sheet
  sheet.getRange(2, 1, outerArray.length, output.length).setValues(outerArray);
};

【讨论】:

  • 感谢您提供的所有帮助。无论我如何查看代码,我仍然无法获取标题 - 可以很好地获取名称、类型、描述和 URL,但对标题选项一点也不满意。
  • 如果文档中没有标题部分,getHeader() 将返回null。作为一个简单的测试,请参阅我的更新答案。
  • 好的,很棒的东西,让它部分工作。我现在收到一些 null 错误。所以,必须有一种简单的方法来忽略空返回,也许添加一个“-”来代替?更新了上面的代码
  • 是的,使用if 条件检查:if (myVariable !== null) {//If value is NOT equal to null, then do something, else do nothing};
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
相关资源
最近更新 更多