首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >google电子表格pdf电子邮件附件

google电子表格pdf电子邮件附件
EN

Stack Overflow用户
提问于 2017-06-19 09:14:22
回答 2查看 66关注 0票数 1

我在脚本中的电子邮件附件有问题。

守则:

代码语言:javascript
复制
//creating a new temporary spreadsheet
var tmpSpreadSheetId = SpreadsheetApp.create("attachment").getId();
var tmpSpreadSheet = SpreadsheetApp.openById(tmpSpreadSheetId);
var tmpSheet = tmpSpreadSheet.getActiveSheet();

//copying values from another spreadsheet with the original data
var values = otherSpreadSheet.getValues();
tmpSheet.getRange(1, 1, otherSpreadSheet.getLastRow(), otherSpreadSheet.getLastColumn()).setValues(values);

//formating some numbers in the temporary spreadsheet
var lastRow = tmpSpreadSheet.getLastRow();
tmpSheet.getRange(lastRow, 1, 1, 10).setBackground('#0000ff').setFontColor('white');
tmpSheet.getRange(1, 1, lastRow, 10).setBorder(null, true, null, true, null, true);
tmpSheet.getRange(1, 1, 1, 10).setBackground('#0000ff').setFontColor('white');
tmpSheet.getRange(2, 3, lastRow, 1).setNumberFormat("00.00%");
tmpSheet.getRange(2, 5, lastRow, 1).setNumberFormat("00.00%");
tmpSheet.getRange(2, 6, lastRow, 2).setNumberFormat("0.00");
tmpSheet.getRange(2, 8, lastRow, 1).setNumberFormat("00.00");

//logging the edited spreadsheet URL
Logger.log(tmpSpreadSheet.getUrl());

//sending an email with the spreadsheet as pdf
var file = DriveApp.getFileById(tmpSheet.getParent().getId()); 
MailApp.sendEmail('example@example.com', 'Reporting', 'bla bla bla', {attachments: [file.getAs(MimeType.PDF)]});

问题是:临时电子表格看起来很好。彩色和正确的格式化数字等等,但在附件中,它只是普通表,格式错误的数字,没有颜色,就像数据一样,在开始插入到tempSheet。

希望你能理解我的问题,我一点也不知道,问题是什么,我试着解决了2-3个小时,但现在我需要帮助:)

提前谢谢,祝文森特一周愉快

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-19 12:38:56

你的剧本快到了。您只需在发送电子邮件之前和完成格式化之后添加SpreadsheetApp.flush(); (请参阅下面的代码)。Utilities.sleep()是@Amit提出的一个很好的建议,但是它将不适用于您的代码,因为它需要刷新才能更新,不幸的是,睡眠只会减慢代码的运行速度,因为它不像setTimeout()那样工作(我已经尝试过了)。

不管怎么说,这是代码,我稍微编辑了一下,但本质上唯一的区别是SpreadsheetApp.flush()。如果您需要进一步的帮助,请告诉我:)

代码语言:javascript
复制
    function myfunction() {
  // otherSpreadSheet details
  var otherSpreadSheet = SpreadsheetApp.openById("id").getSheets()[0];

  //creating a new temporary spreadsheet
  var tmpSpreadSheetId = SpreadsheetApp.create("attachment");
  var tmpSheet = tmpSpreadSheetId.getSheets()[0];

  //copying values from another spreadsheet with the original data
  var values = otherSpreadSheet.getDataRange().getValues();
  tmpSheet
    .getRange(
      1,
      1,
      otherSpreadSheet.getLastRow(),
      otherSpreadSheet.getLastColumn()
    )
    .setValues(values);

  //formating some numbers in the temporary spreadsheet
  var lastRow = tmpSheet.getLastRow(); // **last row had an error in the script here
  tmpSheet
    .getRange(lastRow, 1, 1, 10)
    .setBackground("#0000ff")
    .setFontColor("white");
  tmpSheet
    .getRange(1, 1, lastRow, 10)
    .setBorder(null, true, null, true, null, true);
  tmpSheet.getRange(1, 1, 1, 10).setBackground("#0000ff").setFontColor("white");
  tmpSheet.getRange(2, 3, lastRow, 1).setNumberFormat("00.00%");
  tmpSheet.getRange(2, 5, lastRow, 1).setNumberFormat("00.00%");
  tmpSheet.getRange(2, 6, lastRow, 2).setNumberFormat("0.00");
  tmpSheet.getRange(2, 8, lastRow, 1).setNumberFormat("00.00");

  // USE SpreadsheetApp.flush() to update the sheet before you send it as a PDF,
  // this will allow you to apply the formatting and any other functions.
  SpreadsheetApp.flush();

  var fileID = tmpSheet.getParent().getId();
  var file = DriveApp.getFileById(fileID);
  MailApp.sendEmail("EMAIL HERE", "Reporting", "bla bla bla", {
    attachments: [file.getAs(MimeType.PDF)]
    });
  }
票数 0
EN

Stack Overflow用户

发布于 2017-06-19 11:01:17

在发送电子邮件之前,考虑使用Utilities.sleep()添加一个延迟。还替换:

代码语言:javascript
复制
var file = DriveApp.getFileById(tmpSheet.getParent().getId()); 

通过以下方式:

代码语言:javascript
复制
var file = DriveApp.getFileById(tmpSpreadSheet); 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44626525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档