简体   繁体   English

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

[英]Script generating pdf with old data snapshot from google spreadsheet?

Folks, Anybody can tell me why this code sends a pdf with a snapshot of data prior to the update?伙计们,谁能告诉我为什么此代码在更新之前发送带有数据快照的pdf?

What am I missing here?我在这里缺少什么? Is there a need to save or flush the current file?是否需要保存或刷新当前文件? Any help is appreciated here.任何帮助在这里表示赞赏。

Tks, Ivan Daudt Tks,伊万·道特

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]});
   }
  • You want to know the reason of this code sends a pdf with a snapshot of data prior to the update .您想知道this code sends a pdf with a snapshot of data prior to the update的原因。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样?

Modification points:改装要点:

  • In your script, the data is put to destRange using destRange.setValues(sourceRange.getValues());在您的脚本中,使用destRange.setValues(sourceRange.getValues());将数据放入destRange destRange.setValues(sourceRange.getValues()); . . Although I'm not sure about tab of var sheetNumber = (tab.getIndex()-1);虽然我不确定var sheetNumber = (tab.getIndex()-1); tab , if you want to export destSheet of var destSheet = destFile.getSheetByName("dados do aluno"); , 如果你想导出destSheetvar destSheet = destFile.getSheetByName("dados do aluno"); as a PDF, it is required to put SpreadsheetApp.flush() after destRange.setValues(sourceRange.getValues());作为PDF,需要将SpreadsheetApp.flush()放在destRange.setValues(sourceRange.getValues()); . . By this, the put values are reflected to the PDF data.这样,放置值就会反映到 PDF 数据中。
  • From above situation, about Is there a need to save or flush the current file?从上面的情况来看,关于Is there a need to save or flush the current file? , it's yes. ,是的。

Modified script:修改后的脚本:

When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。

From: 从:
 destRange.setValues(sourceRange.getValues());
To: 到:
 destRange.setValues(sourceRange.getValues()); SpreadsheetApp.flush(); // Added

Reference:参考:

If this was not the direction you want, I apologize.如果这不是您想要的方向,我深表歉意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用脚本从Google电子表格生成Google文档(PDF)时,如何控制日期格式? - How can control the date format when generating a google docs (PDF) from a google spreadsheet with script? 用于电子表格的Google脚本可从工作表上链接的PDF页面提取数据 - Google Script for spreadsheet to extract data from a PDF page linked on the sheet 使用脚本从Google电子表格生成(PDF)时,如何控制日期格式? - How do I control the date format when generating a (PDF) from a google spreadsheet with script? 关于如何从电子表格接收到的数据创建 pdf 并通过 email 发送 pdf 文件的 Google 应用脚本 - Google app script on how to create a pdf from the data received from the spreadsheet and send the pdf file via email 使用 Google Script 将旧数据移动到辅助电子表格 - Moving old data to secondary spreadsheet using Google Script 更新工作表、创建 PDF、pdf 中的旧数据(谷歌应用程序脚本) - Update sheets, create PDF, old data in pdf (google apps script) Google Script将表单数据导入新电子表格,然后以PDF格式发送 - Google Script import form data to new spreadsheet, then send as PDF 关于如何根据从其电子表格的 html 形式收到的数据创建 pdf 并通过电子邮件发送 pdf 文件的 Google 应用脚本 - Google app script on how to create a pdf from the data received from an html form of its spreadsheet and send the pdf file via email 将Google Spreadsheet转换为pdf,并在Google脚本中添加水印 - Google Spreadsheet to pdf with watermark in Google script Google Apps脚本-从电子表格创建PDf会生成Google登录页面的pdf - Google Apps Script - Create PDf from Spreadsheet produces pdf of Google sign in page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM