如何为电子表格的每一行创建一个 google 文档?

How to create a google doc for each row of a spreadsheet?

哟! 这是我的代码,它使用电子表格的最后一行生成 google 文档,此外,它还在源电子表格中为该文档放置了 link。是否可以创建对工作表中的所有行执行此操作的代码?我在使用 .range() 单独读取行时遇到问题。这样做的原因只是为了维护和更新生成的数据与更新的数据。

提前谢谢!

var app=SpreadsheetApp.openById("Google ID");
var sheet=app.getSheetByName('Respostas ao formulário 1')


function autoFillLastRow() {

  var range=sheet.getDataRange().getValues();
  var elem = range.pop();

  //e.values is an array of form values
  var timestamp = elem[0];

  //conversor de datas
  let data = new Date(timestamp);
  let dataFormatada = ((data.getDate() )) + "/" + ((data.getMonth() + 1)) + "/" +   data.getFullYear(); 

  var firstName = elem[1];
  var lastName = elem[2];
  var title = elem[3];
  
  //file is the template file, and you get it by ID
  var file = DriveApp.getFileById('Google Drive ID'); 
  
  //We can make a copy of the template, name it, and optionally tell it what folder to live in
  //file.makeCopy will return a Google Drive file object
  var folder = DriveApp.getFolderById('Google Drive ID')
  var copy = file.makeCopy(dataFormatada + ', ' + firstName, folder); 
  
  //Once we've got the new file created, we need to open it as a document by using its ID
  var doc = DocumentApp.openById(copy.getId()); 
  
  //Since everything we need to change is in the body, we need to get that
  var body = doc.getBody(); 
  
  //Then we call all of our replaceText methods
  body.replaceText('{{Nome}}', firstName); 
  body.replaceText('{{Sobrenome}}', lastName);  
  body.replaceText('{{Título}}', title); 
  
  //Lastly we save and close the document to persist our changes
  doc.saveAndClose(); 

  //edit cell
  var coluna = 5;
  var linha = sheet.getLastRow();
  var docId = doc.getId();
  sheet.getRange(linha, coluna,1,1).setValue("https://docs.google.com/document/d/" + docId);


  }

为电子表格的每一行创建文档

   function createDocforEveryRowofSpreadsheet() {
      var ss = SpreadsheetApp.openById("Google ID");
      var sh = ss.getSheetByName('Respostas ao formulário 1')
      var file = DriveApp.getFileById('Google Drive ID');
      var folder = DriveApp.getFolderById('Google Drive ID')
      var [hA,...vs] = sh.getDataRange().getValues();
      vs.forEach((r,i) =>  {
        var ts = r[0];
        let data = new Date(ts);
        let dataFormatada = Utilities.formatDate(new Date(ts), Spreadsheet.getSpreadsheetTimeZone(), "dd/MM/yyyy")
        var firstName = r[1];
        var lastName = r[2];
        var title = r[3];
        var copy = file.makeCopy(dataFormatada + ', ' + firstName, folder);
        var doc = DocumentApp.openById(copy.getId());
        var body = doc.getBody();
        body.replaceText('{{Nome}}', firstName);
        body.replaceText('{{Sobrenome}}', lastName);
        body.replaceText('{{Título}}', title);
        doc.saveAndClose();
      })
    }