Google文档中的标题部分 [英] Header section within Google Docs

查看:198
本文介绍了Google文档中的标题部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, i have ammended some code to add the description field of a file search to a spreadsheet, which is potentially useful for me. However, ideally I want to conduct a file search and then add the details within the header section of a google document to a spreadsheet column.

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

The problem line I assume is: var header= file.getHeader();

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

How do I query the search performed to return the contents of the document header? Can anyone help with this please?

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);
}
}

推荐答案

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

Manually insert a header into a document, then open the document and get the ID out of the url. Then run some test code:

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

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

从脚本编辑器运行代码,然后在查看"菜单中打开日志".您还可以与调试器逐行运行代码.

Run the code from the script editor, then in the VIEW menu, open the LOGS. You can also run the code line by line with the debugger.

在日志中,应显示"HeaderSection"

In the log, it should show "HeaderSection"


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

You should use DriveApp to get a list of files, but then you'll need to use DocumentApp to get the header.

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);
};

这篇关于Google文档中的标题部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆