【问题标题】:Export a google sheet to CSV and email as attachment -> sends a CSV attachment but is populated with junk将谷歌工作表导出为 CSV 并作为附件发送电子邮件 -> 发送 CSV 附件,但其中填充了垃圾邮件
【发布时间】:2020-08-21 21:01:10
【问题描述】:

我正在尝试将特定工作表导出为 CSV 格式,然后将 csv 文件作为电子邮件附件通过电子邮件发送。我的代码最终可以通过电子邮件发送 CSV 附件,但 csv 附件的内容是伪造的。我记录了 URL,然后直接从浏览器进行测试。它似乎找到了正确的命名表。有没有办法验证UrlFetchApp.fetch() 的结果?谁能告诉我在这里做错了什么?

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var convertedSheet = ss.getSheetByName(dateToProcess);
  if (convertedSheet != null) {
  
    var sheetName = convertedSheet.getName();
    Logger.log("Sheet Name:", sheetName); // logs correct sheet name
    
    // this approach to constructing url is suggested in a similar post instead of using "getUrl()
    var url = 'https://docs.google.com/spreadsheets/d/'+ SpreadsheetApp.getActiveSpreadsheet().getId();
    url += '#gid=';
    url += convertedSheet.getSheetId();
    url += "/export?exportFormat=csv&format=csv";
    
    Logger.log("URL: ", url); //this brings up correct sheet when I paste the url in the browser. It does not trigger a  download of it in the browser - should it be?

    var email = Session.getActiveUser().getEmail();
    Logger.log(email); //correct

    var subject = "New Submission is Attached! ";
    var body = "You have a new submission!";

    var requestData = {method:"GET",headers:{"authorization":"Bearer "+        ScriptApp.getOAuthToken()}};
        
    var result = UrlFetchApp.fetch(url, requestData).getBlob();

    MailApp.sendEmail(email,
                      subject, 
                      body,
                      {
                          attachments: [{
                          fileName: sheetName + ".csv",
                          content: result.getBytes(),
                          mimeType: "text/csv"
                        }]
                      });
    
    }

在作为附件发送的 CSV 中,单元格填充了看似生成的代码(由代码中的所有逗号分隔)。

【问题讨论】:

  • 这里是 URL 的基本格式(减去 id 和域)... h t t p s // docs.google.com/spreadsheets/d//edit?ouid=&urlBuilderDomain=.org#gid=/export?exportFormat=csv&format=csv"
  • 其他解决方案呢? stackoverflow.com/a/63491497
  • 忽略上面的第一条评论...在整合了格式化 url 的建议方法(不使用 'getUrl',在上一篇文章中建议)之后,我得到了可以在我的浏览器中使用的格式:h t t p s : / /docs.google.com/spreadsheets/d/SPREADSHEET_ID#gid=SHEET_ID/export?exportFormat=csv&format=csv
  • 但是脚本也有同样的问题。未解决。

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


【解决方案1】:

通过在异步运行的 api 调用之间添加对 SpreadsheetApp.flush() 的调用解决了我的问题。

【讨论】:

    【解决方案2】:

    在 2022 年 1 月尝试过,包括 flush(),但对我不起作用。

    我的解决方案是简单地将 URL 格式更改为:

    var url = 'https://docs.google.com/spreadsheets/d/' + SpreadSheetId + "/export?format=csv&gid=" + SheetId;
    

    在哪里

    SpeadSheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
    SheetId = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Name of your sheet").getSheetId();
    

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 2018-05-31
      • 2015-12-21
      • 2017-07-06
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多