【问题标题】:Mail range not whole sheet - apps script邮件范围不是整个工作表 - 应用程序脚本
【发布时间】:2020-04-03 18:49:48
【问题描述】:

我有一张 Google 表格。我想把它作为 PDF 邮寄出去。 下面的代码可以将整个工作表(一个选项卡)以 pdf 格式通过电子邮件发送。 &range 不做任何事情。我想要它,但它没有。 问题:如何更改此代码以通过电子邮件发送“选定的单元格”? 额外问题:为什么我必须输入 ?exportFormat=pdf 和 &format=pdf?

      var url_base = ss.getUrl().replace(/edit$/,'');
      var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
      + (sheetId ? ('&gid=' + sheetId) : ('&id=' + ssId))
      + '&range=E1:L25' // <<< THIS DOESN'T SEEM TO DO ANYTHING
      + '&format=pdf'                   //export format

【问题讨论】:

    标签: pdf google-apps-script google-sheets gmail


    【解决方案1】:

    实际上它确实有效。找到我用作基础的源代码的原始定义,我看到我将参数设置为通过电子邮件发送整个表格文件而不是一个选项卡。指向一个选项卡后,&range 参数被激活,我得到了想要的结果。

    电子邮件工作簿或选项卡参考: Google Script - Send active sheet as PDF to email listed in cell

    还有我自己的扩展,包括范围能力和其他一些我认为的改进。

    /*
    shNum = 0 for whole workbook or '0', or 0,1,2,etc for specific tab/sheet
    shRng = A1 address for desired range, eg 'E1:L25', ignored if not a single sheet shNum
    pdfName = text on top of pdf
    */
    function mailPdf(shNum,shRng,pdfName,email,subject,htmlbody) {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var ssId = ss.getId();
      var shId = shNum ? ss.getSheets()[shNum].getSheetId() : null;  
      var url_base = ss.getUrl().replace(/edit$/,'');
      var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
          + (shId ? ('&gid=' + shId) : ('&id=' + ssId))
          + (shRng ? ('&range=E1:L25') : null)
          + '&format=pdf'                   //export format
          + '&size=letter'                      //A3/A4/A5/B4/B5/letter/tabloid/legal/statement/executive/folio
          //+ '&portrait=false'               //true= Potrait / false= Landscape
          //+ '&scale=1'                      //1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
          //+ '&top_margin=0.00'              //All four margins must be set!
          //+ '&bottom_margin=0.00'           //All four margins must be set!
          //+ '&left_margin=0.00'             //All four margins must be set!
          //+ '&right_margin=0.00'            //All four margins must be set!
          + '&gridlines=false'              //true/false
          //+ '&printnotes=false'             //true/false
          //+ '&pageorder=2'                  //1= Down, then over / 2= Over, then down
          //+ '&horizontal_alignment=CENTER'  //LEFT/CENTER/RIGHT
          + '&vertical_alignment=TOP'       //TOP/MIDDLE/BOTTOM
          //+ '&printtitle=false'             //true/false
          //+ '&sheetnames=false'             //true/false
          //+ '&fzr=false'                    //true/false frozen rows
          //+ '&fzc=false'                    //true/false frozen cols
          //+ '&attachment=false'             //true/false
    
      var options = {
        headers: {
          'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
          'muteHttpExceptions': true
        }
      }
    
      var response = UrlFetchApp.fetch(url_base + url_ext, options);
      var blob = response.getBlob().setName(pdfName + '.pdf');
      if (email) {
        var mailOptions = {
          attachments:blob, htmlBody:htmlbody
        }
    
    
    MailApp.sendEmail(
          // email + "," + Session.getActiveUser().getEmail() // use this to email self and others
          email,                                              // use this to only email users requested
          subject+' (' + pdfName +')', 
          'html content only', 
          mailOptions);
    
      }
    }
    

    【讨论】:

    • 嗨!您将如何动态设置范围?
    猜你喜欢
    • 1970-01-01
    • 2018-07-17
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2015-05-30
    相关资源
    最近更新 更多