【问题标题】:Google Scripts Sheets.Spreadsheets.Values.get no longer work in Google Apps Script. What am I missing?Google 脚本 Sheets.Spreadsheets.Values.get 不再在 Google Apps 脚本中工作。我错过了什么?
【发布时间】:2020-11-19 21:10:28
【问题描述】:

自从我制作了这个脚本后,Google Scripts 有了全新的外观,显然是时候将我笨拙的表格更新为 Doc 合并了。当我运行 createDocument() 函数时,我收到“ReferenceError: Sheets is not defined”

谁能让我开始了解缺少的内容(实际的表格和文档 ID 除外)?

function createDocument() {
  var fname = Sheets.Spreadsheets.Values.get('google sheet template id', 'A2:A'); //column A
  var lname = Sheets.Spreadsheets.Values.get('google sheet template id', 'B2:B'); //column B
  var gradeh = Sheets.Spreadsheets.Values.get('google sheet template id', 'H2:H'); //column H

  var templateId = 'google doc template id';
  
  for(var i = 0; i < fname.values.length; i++){
    
    var firstname = fname.values[i][0];
    var lastname = lname.values[i][0];
    var gradeH = gradeh.values[i][0];
    
 //Make a copy of the template file
    var documentId = DriveApp.getFileById(templateId).makeCopy().getId();
    
 //Rename the copied file
    DriveApp.getFileById(documentId).setName('Year ' + firstname + ' ' + lastname + ' Report');
    
 //Get the document body as a variable
    var body = DocumentApp.openById(documentId).getBody();
    
 //replace template text with spreadsheet data
    
    body.replaceText('##gradeh##', gradeH)
    body.replaceText('##first##', firstname)
    body.replaceText('##last##', lastname)
   
  }

}

【问题讨论】:

  • 您是否在 Google 高级服务中启用了表格?
  • 我做了,但没有得到任何改变。我从表格创建了一个新脚本并重写了代码,该脚本正在运行。谢谢。

标签: google-apps-script google-sheets google-docs


【解决方案1】:

如果有人有需要更新的旧合并:

    function myFunction() {
  
  var templateId = "doc template id";
  var mergesheetId = "google sheet template id";
  var finalId = "doc finished id"
  
  var docTemp = DocumentApp.openById(templateId);
  var docFinal = DocumentApp.openById(finalId);
  var ws = SpreadsheetApp.openById(mergesheetId).getSheetByName("data");
  
  var data = ws.getRange(2, 1,ws.getLastRow()-1,12).getValues();
  
  var templateParagraphs = docTemp.getBody().getParagraphs();
  
  docFinal.getBody().clear();
  
  data.forEach(function(r){
    createMailMerge(r[8],r[9],r[10],r[11],r[0],r[1],r[7],templateParagraphs,docFinal)
  
  });
    

}

function createMailMerge(comment1,comment2,comment3,comment4,first,last,grade,templateParagraphs,docFinal){
  
  templateParagraphs.forEach(function(p){
    docFinal.getBody().appendParagraph(
      p
      .copy()
      .replaceText("{comment1}", comment1)
      .replaceText("{comment2}", comment2)
      .replaceText("{comment3}", comment3)
      .replaceText("{comment4}", comment4)
      .replaceText("{first}", first)
      .replaceText("{last}", last)
      .replaceText("{grade}", grade)
      
    );
  });
  
  docFinal.getBody().appendPageBreak();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多