将在特定文件夹中找到的所有文档正文复制到 Google 电子表格中

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

我正在尝试创建一个脚本来获取 Google Drive 文件夹中的所有(Google Doc)文档 ID,从每个文档中获取正文,然后 returns这变成 this spreadsheet 中的行。

以下语法获取文件 ID 和名称,但不获取正文:

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

我认为问题出在 file.getBlob 我试过了 file.getBlob.getDataAsString(),但是 returns:

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

如何检索每个文档正文的 text

由于我们感兴趣的是 Google 文档,因此请将文件扫描重点放在这些文档上:

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

然后在使用文件迭代器时,将文件 ID 与 DocumentApp 一起使用以将文件作为 Google 文档打开 - 这样,正文将很容易访问。

更新代码:

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