【问题标题】:How do I attach a working pdf of my google sheet to a draft email?如何将我的谷歌工作表的工作 pdf 附加到草稿电子邮件?
【发布时间】:2020-09-03 23:35:02
【问题描述】:

我有一个脚本,用于打开、重命名和将谷歌工作表导出为 pdf。然后该表将被添加到电子邮件草稿中。我已经使用这个脚本两年了,但由于某种原因,pdf 附件将不再打开。

我收到的错误消息是“Adobe Acrobat Reader 无法打开 [文件名.pdf],因为它不是受支持的文件类型或文件已损坏。”我直接去谷歌表并手动导出为pdf,我可以毫无问题地打开它,所以它肯定是与脚本绑定的。

能否请您查看以下脚本,看看是否有我可以使用的解决方法?

 // Note that the spreadsheet is NOT physically opened on the client side.
 // It is opened on the server only (for modification by the script).
 var ss = SpreadsheetApp.openByUrl(Logger.getLog());
 var first = ss.getSheetByName("BHC AP Vendor Payment");
    var vendorname = first.getRange(2, 1).getValue();
  var paymentdate = first.getRange(2, 22).getDisplayValue();
 // renames the google sheet to the data that is in 2nd Row, 1st Column
  ss.rename('BHC '+vendorname+' Payment ['+paymentdate+']');
  
  
  var sheet = ss.getSheetByName('BHC AP Vendor Payment'); //returns the sheet named 'BHC AP Vendor Payment'
  sheet.setName('BHC '+vendorname+' Payment ['+paymentdate+']');
  var lastRow = sheet.getLastRow(); //returns integer last row
  
    var url = ss.getUrl();

  //remove the trailing 'edit' from the url
  url = url.replace(/edit$/, '');

  //additional parameters for exporting the sheet as a pdf
  var url_ext = 'export?exportFormat=pdf&format=pdf' + //export as pdf
    //below parameters are optional...
    '&size=letter' + //paper size
    '&portrait=false' + //orientation, false for landscape
    '&fitw=true' + //fit to width, false for actual size
    '&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional headers and footers
    '&gridlines=false' + //hide gridlines
    '&fzr=false' + //do not repeat row headers (frozen rows) on each page
    '&gid=' + sheet.getSheetId(); //the sheet's Id

  var token = ScriptApp.getOAuthToken();

  var response = UrlFetchApp.fetch(url + url_ext, {
    headers: {
      'Authorization': 'Bearer ' + token
    }
  });

  var blob = response.getBlob().setName(ss.getName() + '.pdf');


     // Create a draft email with a file from Google Drive attached as a PDF.
  var doc = DocumentApp.openById(*sample document*);
 var body = doc.getBody();
  var bodytext = body.getText();

  var apEmail = *sample email address*;
    var subject = 'Payment ['+vendorname+']';
   
  
  GmailApp.createDraft('', subject, bodytext, {
    cc: apEmail,
    from: apEmail,
        attachments: [blob
        ]

谢谢!

【问题讨论】:

  • 在您的情况下,当var blob = response.getBlob().setName(ss.getName() + '.pdf')blob 被创建为PDF 文件时,创建的PDF 文件是您期望的正确PDF 文件吗?
  • 当我查看带有附件的电子邮件草稿时,它显示为“.pdf”,当我将其保存到我的计算机以打开文件时,它在属性中列为 pdf .
  • 感谢您的回复。从您的回复中,我无法理解The error message I get is "Adobe Acrobat Reader could not open [File Name.pdf] because it is either not a supported file type or because the file has been damaged."。我为我糟糕的英语水平道歉。我能问一下您当前问题的详细信息吗?
  • 感谢您的提问。当我运行脚本时,我成功地获得了带有 pdf 附件的电子邮件草稿。但是,当我下载该 pdf 附件并尝试打开它时,我收到一条错误消息。如果我尝试在 Google Chrome 中打开它,错误消息是“无法加载 PDF 文档”。如果我尝试在 Adob​​e Acrobat Reader 中打开它,我会收到错误消息“Adobe Acrobat Reader 无法打开 [文件名.pdf],因为它不是受支持的文件类型或文件已损坏。”这更有意义吗?如果没有,我可以找到另一种方式来解释这个问题。
  • 感谢您的回复。现在我注意到你的问题已经解决了。我很高兴。

标签: email pdf google-apps-script google-sheets format


【解决方案1】:

修改:

请您尝试以下两个修改点:

  • 代替:

    url = url.replace(/edit$/, '');
    

    使用:

    url = url.replace(/\/edit.*$/, '');
    

  • 而不是:

    var url_ext = 'export?...'
    

    使用:

    var url_ext = '/export?...'
    

完整代码:

 var ss = SpreadsheetApp.openByUrl(Logger.getLog());
 var first = ss.getSheetByName("BHC AP Vendor Payment");
    var vendorname = first.getRange(2, 1).getValue();
  var paymentdate = first.getRange(2, 22).getDisplayValue();
 // renames the google sheet to the data that is in 2nd Row, 1st Column
  ss.rename('BHC '+vendorname+' Payment ['+paymentdate+']');
  
  
  var sheet = ss.getSheetByName('BHC AP Vendor Payment'); //returns the sheet named 'BHC AP Vendor Payment'
  sheet.setName('BHC '+vendorname+' Payment ['+paymentdate+']');
  var lastRow = sheet.getLastRow(); //returns integer last row
  


  //remove the trailing 'edit' from the url
  url = url.replace(/\/edit.*$/, '');


  //additional parameters for exporting the sheet as a pdf
  var url_ext = '/export?exportFormat=pdf&format=pdf' + //export as pdf
    //below parameters are optional...
    '&size=letter' + //paper size
    '&portrait=false' + //orientation, false for landscape
    '&fitw=true' + //fit to width, false for actual size
    '&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional headers and footers
    '&gridlines=false' + //hide gridlines
    '&fzr=false' + //do not repeat row headers (frozen rows) on each page
    '&gid=' + sheet.getSheetId(); //the sheet's Id

  var token = ScriptApp.getOAuthToken();

  var response = UrlFetchApp.fetch(url + url_ext, {
    headers: {
      'Authorization': 'Bearer ' + token
    }
  });

  var blob = response.getBlob().getAs('application/pdf').setName(ss.getName() + '.pdf');


     // Create a draft email with a file from Google Drive attached as a PDF.
  var doc = DocumentApp.openById(*sample document*);
 var body = doc.getBody();
  var bodytext = body.getText();

  var apEmail = *sample email address*;
    var subject = 'Payment ['+vendorname+']';
   
  
  GmailApp.createDraft('', subject, bodytext, {
    cc: apEmail,
    from: apEmail,
        attachments: [blob
        ]})

【讨论】:

  • 另一个选项如下:url = url.replace(/\/edit.*$/, '');var url_ext = '/export?...'
  • @Iamblichus 感谢您的推荐,我会给 OP 一些时间来尝试我的答案,然后我会更新它以包含您的答案。再次感谢!
  • @Marios 我尝试替换您上面提到的片段,但不幸的是,当我运行脚本时,我不再收到 gmail 中的电子邮件草稿。
  • 你可以只试其中一个吗?单独的第一个或第二个修改。 @MeredithMiller
  • 好的,我尝试了您的解决方案,@Marios,不幸的是它没有创建草稿,无论是同时进行更改还是单独尝试一个。好消息是lamblichus的想法奏效了!我现在可以下载 pdf,它会毫无问题地打开。我真的很感谢你们俩为此付出的努力!你们俩都很棒!谢谢!
猜你喜欢
  • 2020-09-07
  • 2019-06-05
  • 2017-07-06
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
相关资源
最近更新 更多