javascript - 通过电子邮件发送谷歌电子表格范围作为 HTML 表保留值?

标签 javascript google-apps-script google-sheets

我修改了一个脚本,它的作用是。

它获取电子表格范围,然后获取所有值并将它们重新格式化为通过电子邮件发送的 HTML 表格。到目前为止,所有这些都有效。异常(exception):它显示的数字使用没有小数点的数字格式。例如,原始电子表格的格式化值为 10,249.2,该值已四舍五入,但电子邮件中的 HTML 表格显示:10249.243990558198

这是我正在使用的代码:

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Send EOD",
    functionName : "sendEmail"
  }];
  sheet.addMenu("Send Email", entries);
};

function sendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
 // adding for range
  var lastRow = sheet.getLastRow();
  var lastColumn = sheet.getLastColumn();
 //  var schedRange = sheet.getRange("T10:AC"+lastColumn);
   var schedRange = sheet.getRange("T10:AC28");
 // end of range 
  var range = sheet.getDataRange();
  var formattedDate = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yyyy")
  var subject = "Daily Volume - " + formattedDate;

  // Dynamically get whole sheet based on lastrow and lastcolumn
  // var schedRange = sheet.getRange(1, 1, lastRow, lastColumn);

  // We only want the schedule within borders, so
  // these are handled separately.
  var body = '<div style="text-align:center;display: inline-block;font-family: arial,sans,sans-serif">'
  body += '<H1>'+ "Daily Volume   " + formattedDate  +'</H1>';
  body += '<H2>'
       + '</H2>';
  body += getHtmlTable(schedRange);
  body += '</div>';
  debugger;
  //Logger.log(body);
  recipient = 'my@email.com'+","+"mysecondemail@email.com";
  GmailApp.sendEmail(recipient, subject, "Requires HTML", {htmlBody:body})
}

/**
 * Return a string containing an HTML table representation
 * of the given range, preserving style settings.
 */
function getHtmlTable(range){
  var ss = range.getSheet().getParent();
  var sheet = range.getSheet();
  startRow = range.getRow();
  startCol = range.getColumn();
  lastRow = range.getLastRow();
  lastCol = range.getLastColumn();
  // testing
 // var lastRow = sheet.getLastRow();
  // var lastColumn = sheet.getLastColumn();
  // var schedRange = sheet.getRange("T10:AC"+lastColumn);
  //end of testing 

  // Read table contents
  var numberFormats = range.getNumberFormats();
  var data = range.getValues();


  // Get css style attributes from range
  var fontColors = range.getFontColors();
  var backgrounds = range.getBackgrounds();
  var fontFamilies = range.getFontFamilies();
  var fontSizes = range.getFontSizes();
  var fontLines = range.getFontLines();
  var fontWeights = range.getFontWeights();
  var horizontalAlignments = range.getHorizontalAlignments();
  var verticalAlignments = range.getVerticalAlignments();

  // Get column widths in pixels
  var colWidths = [];
  for (var col=startCol; col<=lastCol; col++) {
    colWidths.push(sheet.getColumnWidth(col));
  }
  // Get Row heights in pixels
  var rowHeights = [];
  for (var row=startRow; row<=lastRow; row++) {
    rowHeights.push(sheet.getRowHeight(row));
  }

  // Future consideration...
  // var numberFormats = schedRange.getNumberFormats();





  // Build HTML Table, with inline styling for each cell
  var tableFormat = 'style="border:1.5px solid black;border-collapse:collapse;text-align:center" border = 1.5 cellpadding = 5';
  var html = ['<table '+tableFormat+'>'];
  // Column widths appear outside of table rows
  for (col=0;col<colWidths.length;col++) {
    html.push('<col width="'+colWidths[col]+'">')
  }
  // Populate rows
  for (row=0;row<data.length;row++) {
    html.push('<tr height="'+rowHeights[row]+'">');
    for (col=0;col<data[row].length;col++) {
      // Get formatted data
      var cellText = data[row][col];
      if (cellText instanceof Date) {
        cellText = Utilities.formatDate(
                     cellText,
                     ss.getSpreadsheetTimeZone(),
                     'MMM/d EEE');
      }
      var style = 'style="'
                + 'color: ' + fontColors[row][col]+'; '
                + 'font-family: ' + fontFamilies[row][col]+'; '
                + 'font-size: ' + fontSizes[row][col]+'; '
                + 'font-weight: ' + fontWeights[row][col]+'; '
                + 'background-color: ' + backgrounds[row][col]+'; '
                + 'text-align: ' + horizontalAlignments[row][col]+'; '
                + 'vertical-align: ' + verticalAlignments[row][col]+'; '
                +'"';
      html.push('<td ' + style + '>'
                +cellText
                +'</td>');
    }
    html.push('</tr>');
  }
  html.push('</table>');

  return html.join('');
}

最佳答案

如果您想要屏幕上显示的值,请使用 getDisplayValues() 而不是 getValues()。

相关

关于javascript - 通过电子邮件发送谷歌电子表格范围作为 HTML 表保留值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48104198/

相关文章:

google-apps-script - 我可以使用Google Apps脚本为Google文档中的某些单词着色吗?

javascript - Ace 编辑器 JavaScript : How to disable all extra commands that aren't default in textarea?

javascript - VueJs 模块构建失败 : Error: Couldn't find preset "@vue/app" relative to directory

javascript - Google 脚本 'GetValue' 无法读取电子表格中用 'Query' 填充的值

javascript - 为什么我的循环在带有 Google Apps 脚本的 Google 表格中仅运行一次?

xpath - 在Google电子表格中使用ImportXML提取标题

javascript - 如何防止在工作表上手动更改(不是由脚本编写的)?

javascript - 选中复选框时,Google Sheets 将行记录到另一张工作表(同一本书)

php - 如何在 php 中对大 ajax 响应应用条件?

javascript - IOS Safari trim 大量 localStorage 数据、IONIC2 应用程序