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