首页 > 解决方案 > 错误:TypeError:doc.saveAndClose 不是函数

问题描述

当我运行我的代码时,我遇到了错误“错误:TypeError:doc.saveandClose 不是函数”。我无法在我的编码中发现错误:

function myFunction() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('AutoFill Docs');
  menu.addItem('Create New Docs', 'CreateNewGoogleDocs')
  menu.addToUi();
}

function CreateNewGoogleDocs() {

  const googleDocTemplate = DriveApp.getFileById("1sFPHjQZiwpby0MP_fWmd4-Xkb6rsfcNzXFyaTQrDERU");
  const destinationFolder = DriveApp.getFolderById("10z_aBfhd0J4F5qx9O1T2EGIKAnLCYrf6");
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Mail Merge');
  const rows = sheet.getDataRange().getValues();
  
  rows.forEach(function(row, index) { 
    if (index === 0) return;
    
    const copy = googleDocTemplate.makeCopy(`${row[1]}, Developer Details`, destinationFolder);
    const doc = DocumentApp.openById(copy.getId())
    const body = doc.getBody();
    
    body.replaceText('{{Developer Name}}', row[0]);

    doc.saveAndClose();
    const url = doc.getUrl();
    sheet.getRange(index + 1, 9).setValue(url) 

  })

}

标签: javascriptgoogle-apps-script

解决方案


推荐阅读