Google 文档中的 Header 部分
Header section within Google Docs
所以,我修改了一些代码,将文件搜索的描述字段添加到电子表格中,这对我来说可能很有用。但是,理想情况下,我想进行文件搜索,然后将 google 文档的 header 部分中的详细信息添加到电子表格列中。
我假设的问题行是:var header= file.getHeader();
如何查询对 return 文档 header 的内容执行的搜索?有人可以帮忙吗?
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);
}
}
在文档中手动插入一个header,然后打开文档并从url中获取ID。然后运行一些测试代码:
function getHeader() {
var doc = DocumentApp.openById('your ID here');
var hdear = doc.getHeader();
Logger.log('hdear: ' + hdear);
};
运行 脚本编辑器中的代码,然后在VIEW 菜单中,打开LOGS。您还可以使用调试器逐行 运行 代码。
在日志中,应该显示"HeaderSection"
您应该使用 DriveApp 来获取文件列表,但是您需要使用 DocumentApp
来获取 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 文档的 header 部分中的详细信息添加到电子表格列中。
我假设的问题行是:var header= file.getHeader();
如何查询对 return 文档 header 的内容执行的搜索?有人可以帮忙吗?
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);
}
}
在文档中手动插入一个header,然后打开文档并从url中获取ID。然后运行一些测试代码:
function getHeader() {
var doc = DocumentApp.openById('your ID here');
var hdear = doc.getHeader();
Logger.log('hdear: ' + hdear);
};
运行 脚本编辑器中的代码,然后在VIEW 菜单中,打开LOGS。您还可以使用调试器逐行 运行 代码。
在日志中,应该显示"HeaderSection"
您应该使用 DriveApp 来获取文件列表,但是您需要使用 DocumentApp
来获取 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);
};