优化电子表格数据

时间:2021-05-11 14:54:40

标签: google-apps-script google-sheets optimization google-drive-api google-docs

我正在编写一个脚本,该脚本从谷歌电子表格中提取数据并从该数据生成谷歌文档。 该脚本似乎有效,但五分钟不足以完成我所有的文件,有人可以指导我朝着正确的方向前进吗?

function createDocument() {
  var sheetsURL="";

  var title = Sheets.Spreadsheets.Values.get(sheetsURL, 'B1:B191');
  var ksvalue = Sheets.Spreadsheets.Values.get(sheetsURL, 'A1:A191');
  var q1 = Sheets.Spreadsheets.Values.get(sheetsURL, 'I1:I191');
  var q2 = Sheets.Spreadsheets.Values.get(sheetsURL, 'J1:J191');
  var q3 = Sheets.Spreadsheets.Values.get(sheetsURL, 'K1:K191');
  var q4 = Sheets.Spreadsheets.Values.get(sheetsURL, 'L1:L191');
  var templateId = '';
  
 for(var i = 0; i < title.values.length; i++){
    
    //declare vars to call later
    var titles = title.values[i][0];
    var ksv= ksvalue.values[i][0];
    var q1s = q1.values[i][0];
    var q2s = q2.values[i][0];
    var q3s = q3.values[i][0];
    var q4s = q4.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('' + titles + ' '+ ksv +'');
    
    //Get the document body as a variable
    var body = DocumentApp.openById(documentId).getBody();
    
    
    //replaces tags from the doc with the cells value
    body.replaceText('##TITLE##', titles)
    body.replaceText('##QUESTION1##', q1s)
    body.replaceText('##QUESTION2##', q2s)
    body.replaceText('##QUESTION3##', q3s)
    body.replaceText('##QUESTION4##', q4s)
    
  }

}

2 个答案:

答案 0 :(得分:0)

在这种情况下,我通常的方法是添加一些其他列,在其中将状态设置为已处理,如果超时,我会在下次运行时跳过这些行。

例如,假设在您的情况下,我们添加了第 12 (M) 列,如果创建了该文件的模板,该列将包含“完成”。

在这种情况下,如果脚本超时,在下一次运行时它会从它停止的点恢复。

这是您的情况的近似示例

function createDocument() {
 var start = Date.now();

 function isLate() {
    // Script default timeout is 6 min, so after 5 we shutdown gracefully
    return (Date.now() - start) > 5 * 60 * 1000 
 }

 var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1") 
 var rows = sheet.getDataRange();

 rows.map(function(row, index) {
    // ---> Here we skip the files already created
    if(row[12] !== 'Done' && ! isLate()) {
        // Make a copy of the template file
        var doc = DriveApp.getFileById(templateId).makeCopy();
        
        // Rename the copied file
        doc.setName('' + titles + ' '+ row[0] +'');
        
        // Get the document body as a variable
        var body = doc.getBody();        
        
        //replaces tags from the doc with the cells value
        body.replaceText('##TITLE##', row[1])
        body.replaceText('##QUESTION1##', row[8])
        body.replaceText('##QUESTION2##', row[9])
        body.replaceText('##QUESTION3##', row[10])
        body.replaceText('##QUESTION4##', row[11])

        doc.saveAndClose();

        // --> Here we mark this file as created, to skip on next run
        sheet.getRange(index + 1, 12).setValue('Done');
    }
            
  });

}

您实际上可以删除 isLate 检查,但在这种情况下脚本将错误结束。 IsLate() 检查使其在超时时更优雅地关闭

还没有运行它,所以不确定它是否可以 100% 开箱即用,但您可以大致了解一下。

答案 1 :(得分:0)

这可能还不够,但您可以通过在脚本制作副本时将名称分配给 Google 文档来延长一些执行时间。换句话说,而不是

//Make a copy of the template file
var documentId = DriveApp.getFileById(templateId).makeCopy().getId();

//Rename the copied file
DriveApp.getFileById(documentId).setName('' + titles + ' '+ ksv +'');

使用

//Make a copy of the template file and set its name 
var documentId = DriveApp.getFileById(templateId).makeCopy(titles + ' '+ ksv +'').getId();

此外,您可以通过将 Done 一次添加到多个单元格而不是一个一个地执行此操作来节省时间,只需确保在超出执行时间之前添加此值即可。

相关