带有HTML正文和附件表格式的电子邮件表预览

时间:2018-07-30 12:24:43

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

我已将this solution用作电子邮件发送Google工作表的行作为HTML正文的一部分。

不幸的是,由于HTML主体上设置了限制,因此它会出错。我数据中的行数是动态的。

  

超出限制:电子邮件正文大小。 (第209行,文件“ SideBar”)

有办法解决这个问题吗?我可以在HTML主体上提供行的预览(例如,所有列为10行),然后提供一个链接来查看其余行。由于工作表上的内容会更改,因此链接不应指向该工作表。取而代之的是,我想将工作表的副本作为新文件保存在自己的驱动器上并链接到该文件。另一种选择是附加具有所有行的HTML文件。

这是我目前拥有的:

function emailBreakdown(emailUser, bodyAdd){
  SpreadsheetApp.getActiveSpreadsheet().toast('Please wait while information is refreshed.');
  if(emailUser == null){emailUser = 'xxxxx@yyyyyy.com'}
  if(bodyAdd == null){bodyAdd = 'Testing running of function on script editor'}
  var ss = SpreadsheetApp.getActive();
  var detailsSht = ss.getSheetByName("Details");
  var dataRange = detailsSht.getDataRange();
  var curDate = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yyyy");
  var subject = 'Summary - Exported Data ' + curDate;
  var body = '<br />---------------------------------------------------------------<br />You have received an export of a dataset from the <a href="google.com">Summary</a> dashboard. Please see below:<br /><br />' //provide link to the whole dashboard.

  convSheetAndEmail(dataRange, emailUser, body, bodyAdd, subject);
}

function convSheetAndEmail(rng, emailUser, body, bodyAdd, subject){
  var HTML = SheetConverter.convertRange2html(rng);
  MailApp.sendEmail(emailUser, subject, '', {htmlBody : bodyAdd + body + HTML});
}

1 个答案:

答案 0 :(得分:2)

以下是我通过进一步研究能够汇编的代码。它运作良好,可以满足我的要求。这是它的作用:

  • 附上一张纸。在这种情况下,xls文件。通过HTML进行选择。允许用户在需要时在excel中进行操作。
  • 在电子邮件正文中以HTML格式提供10行预览。
  • 预览和附件保留格式。

无法解决的问题:

  • 将文件保存到用户的个人驱动器。

在这里:

function emailBreakdown(emailUser, bodyAdd){
  SpreadsheetApp.getActiveSpreadsheet().toast('Please wait while information is refreshed.');

  //If running on script editor the variables will not be transferred. Assign values below:
  if(emailUser == null){emailUser = 'xxxxxx@yyyyyyy.com'}
  if(bodyAdd == null){bodyAdd = 'Testing running of function on script editor'}

  var ss = SpreadsheetApp.getActive();
  var detailsSht = ss.getSheetByName('Details');
  var dataRange = detailsSht.getRange('A1:FS11'); //For the preview we are only looking at the first 10 rows of data.
  var curDate = Utilities.formatDate(new Date(), 'GMT+1', 'MM/dd/yyyy');

  //Gather data to convert specific sheet to excel document so it can be attached to the e-mail
  var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
  var sheetID = detailsSht.getSheetId().toString()
  var requestData = {'method': "GET", 'headers':{'Authorization':'Bearer ' + ScriptApp.getOAuthToken()}};
  var url = 'https://docs.google.com/spreadsheets/d/' + ssID + '/export?format=xlsx&id=' + ssID + '&gid=' + sheetID;
  var result = UrlFetchApp.fetch(url , requestData);  
  var contents = result.getContent();

  //Assemble E-mail components
  var subject = 'Summary - Exported Data ' + curDate;
  var body = bodyAdd + 
    '<br /><br /><hr style="border: none;border-top: 3px double #333;color: #333;overflow: visible;text-align: center;height: 5px;"><br />' +
    'You have received an export of a dataset from the <a href="https://docs.google.com/spreadsheets/d/' + ssID + '/">Summary</a> dashboard. Below is a preview of the dataset:<br /><br />'
  var afterBody = '<br /><br /><b>You can view the full dataset through the attached XLS file.</b>'

  convSheetAndEmail(ss, contents, dataRange, emailUser, body, afterBody, subject);
};

function convSheetAndEmail(ss, contents, rng, emailUser, body, afterBody, subject){
  var HTML = SheetConverter.convertRange2html(rng);

  //Send email
  MailApp.sendEmail(
    emailUser, 
    subject, 
    '',{
     htmlBody : body + HTML + afterBody,
     name: 'Full Data Export',
     attachments:[{fileName:'Export Data - ' + ss.getName() + '.xls', content:contents, mimeType:'application//xls'}]
    }
  );
};

除了问题中列出的资源之外,我还从here.借来了代码