脚本使用Google电子表格中的旧数据快照生成pdf吗?

问题描述 投票:0回答:1

伙计,谁能告诉我为什么此代码在更新之前会发送包含数据快照的pdf文件?

我在这里想念什么?是否需要保存或刷新当前文件?在此感谢您的任何帮助。

Tks,Ivan Daudt

function onFormSubmit(e) {
      var responses = e.namedValues;
      var s = SpreadsheetApp.getActiveSheet();  
      var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
      var row = s.getActiveCell().getRow(); //identify last row
      var sourceRange = s.getRange(row,1,1,38); //Get the soure range.

      //open target file and updates its data
      var destFile = SpreadsheetApp.openById("1c8NepY_2ZDxrRVzrhbax4VRPGarqb59eir8wNQ7w4qc");
      SpreadsheetApp.setActiveSpreadsheet(destFile);
      var destSheet = destFile.getSheetByName("dados do aluno");
      var destRange = destSheet.getRange(2,1,1,38); //Gets the destination sheet range.
      destRange.setValues(sourceRange.getValues()); //Gets source sheet value and uses it to set the imported sheet value.
      //send by email
      var sheetNumber = (tab.getIndex()-1);
      var sheetId = sheetNumber ? destFile.getSheets()[sheetNumber].getSheetId() : null;  
      var url_base = destFile.getUrl().replace(/edit$/,'');

      var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
          + (sheetId ? ('&gid=' + sheetId) : ('&id=' + destFile.getId())) 
          // following parameters are optional...
          + '&size=A4'      // paper size
          + '&portrait=true'    // orientation, false for landscape
          + '&fitw=true'        // fit to width, false for actual size
          + '&sheetnames=false&printtitle=false&pagenumbers=true'  //hide optional headers and footers
          + '&gridlines=false'  // hide gridlines
          + '&fzr=false';       // do not repeat row headers (frozen rows) on each page

      var options = {
        headers: {
          'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
        }
      }
      var response = UrlFetchApp.fetch(url_base + url_ext, options);
      var thePdf = response.getBlob().setName('Avaliação '+ onome + '.pdf');  
      MailApp.sendEmail(
        mailtoaddr[0],
        subject,
        message,
        {attachments: [thePdf]});
   }
google-apps-script pdf-generation google-sheets-api
1个回答
0
投票
  • 您想知道this code sends a pdf with a snapshot of data prior to the update的原因。

如果我的理解是正确的,那么这个答案呢?

修改点:

  • 在您的脚本中,使用destRange将数据放入destRange.setValues(sourceRange.getValues());。尽管我不确定tabvar sheetNumber = (tab.getIndex()-1);,但是如果要将destSheetvar destSheet = destFile.getSheetByName("dados do aluno");导出为PDF,则需要将SpreadsheetApp.flush()放在destRange.setValues(sourceRange.getValues());之后。这样,看跌期权价值就会反映到PDF数据中。
  • 从上述情况出发,大约是Is there a need to save or flush the current file?,是。

修改的脚本:

修改脚本后,请进行如下修改。

从:
destRange.setValues(sourceRange.getValues());
至:
destRange.setValues(sourceRange.getValues());
SpreadsheetApp.flush();  // Added

参考:

如果这不是您想要的方向,我很抱歉。

© www.soinside.com 2019 - 2024. All rights reserved.