邮件范围不整张-应用程序脚本 [英] Mail range not whole sheet - apps script

查看:74
本文介绍了邮件范围不整张-应用程序脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google表格.我想以PDF的形式邮寄出去. 以下代码可通过电子邮件以电子邮件形式发送整个表格(一个选项卡). & range不执行任何操作.我想要,但不是. 问题:如何更改此代码,以便通过电子邮件发送选定的单元格"? 奖励问题:为什么我必须放?exportFormat = pdf和& format = pdf?

I have a Google Sheet. I'd like to mail that out as a PDF. The below code works to email the WHOLE sheet (one tab) as pdf. The &range doesn't do anything. I want it to, but it doesn't. Question: How can I change this code so it emails 'selected cells'? Bonus Question: why do I have to put ?exportFormat=pdf and &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

推荐答案

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

Actually it does work. Finding the original definition of the source code I used as a base, I saw that I had the parameter set to email the whole Sheets file rather than one tab. After pointing to one tab, the &range parameter activated, and I got the desired result.

电子邮件工作簿或标签参考: Google Script-以PDF格式发送活动表发送单元格中列出的电子邮件

Email workbook or tabs reference: Google Script - Send active sheet as PDF to email listed in cell

还有我自己的扩展功能,我认为还有其他一些改进.

And my own expansion with range capabilities and some other, I think, improvements.

/*
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);

  }
}

这篇关于邮件范围不整张-应用程序脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆