Josh
Josh

Reputation: 321

Copy the body of all documents found in a specific folder into a Google Spreadsheet

I am trying to create a script that obtains all (Google Doc) document IDs within a Google Drive folder, grabs the body from each of those documents, and returns this into rows in this spreadsheet.

The following syntax gets me the file IDs and Names, but doesn't grab the body:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi().createAddonMenu();  
  ui.addItem('List all body components of files inside a folder without including subfolders', 'list_all_body_components_of_files_inside_one_folder_without_subfolders')     
      .addToUi();
}

function list_all_body_components_of_files_inside_one_folder_without_subfolders() {
  var sh = SpreadsheetApp.getActiveSheet();

 // Prompt the user for a google drive folder ID
  var folderID = Browser.inputBox("Enter the folder ID to search in - It's the bit in the URL after /folders/:");

// return the folder identified by that ID number for further manipulation
  var folder = DriveApp.getFolderById(folderID); 

// print the list to columns A through C 
var list = [];
  list.push(['Body','Name','ID']);
  var files = folder.getFiles();
  while (files.hasNext()){
    file = files.next();
    var row = []
    row.push(file.getBlob(),file.getName(),file.getId())
    list.push(row);
  }
  sh.getRange(1,1,list.length,list[0].length).setValues(list);
}

I think that the problem is with file.getBlob I've tried file.getBlob.getDataAsString(), but that returns:

TypeError: Cannot find function getDataAsString in object function getBlob() {/* */}.

How do I retrieve the text of each document body?

Upvotes: 2

Views: 114

Answers (1)

Mogsdad
Mogsdad

Reputation: 45750

Since it's Google Docs that we're interested in, focus the file scan on those:

var files = folder.getFilesByType(MimeType.GOOGLE_DOCS);

Then when working through the file iterator, use the file ID with DocumentApp to open the file as a Google Document - that way, the body will be readily accessible.

Updated code:

function list_all_body_components_of_files_inside_one_folder_without_subfolders() {
  var sh = SpreadsheetApp.getActiveSheet();

 // Prompt the user for a google drive folder ID
  var folderID = Browser.inputBox("Enter the folder ID to search in - It's the bit in the URL after /folders/:");

// return the folder identified by that ID number for further manipulation
  var folder = DriveApp.getFolderById(folderID); 

// print the list to columns A through C 
var list = [];
  list.push(['Body','Name','ID']);
  var files = folder.getFilesByType(MimeType.GOOGLE_DOCS);
  while (files.hasNext()){
    var fileId = files.next().getId();
    var doc = DocumentApp.openById(fileId);
    var row = []
    row.push(doc.getBody().editAsText().getText(),
             doc.getName(),
             fileId)
    list.push(row);
  }
  sh.getRange(1,1,list.length,list[0].length).setValues(list);
}

Upvotes: 1

Related Questions