简体   繁体   English

如何在Google工作表中以正确的格式邮寄单元格范围?

[英]How do I mail cell range with proper format in google sheet?

Hi this is my sheet: https://docs.google.com/spreadsheets/d/1xkTt_1kLyROkUFComi8-NiKyDMan6617C1dA5UqWTSI/edit?usp=sharing 嗨,这是我的工作表: https : //docs.google.com/spreadsheets/d/1xkTt_1kLyROkUFComi8-NiKyDMan6617C1dA5UqWTSI/edit?usp=sharing

I want to share range A2:F8 in email using google script. 我想使用Google脚本在电子邮件中共享范围A2:F8。 As far as I can say this script works fine: 据我所知,此脚本可以正常工作:

function sendMail(){
 var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var data = sh.getRange("A2:O38").getValues();
  //var htmltable =[];
var TABLEFORMAT = 'cellspacing="2" cellpadding="2" dir="ltr" border="1" style="width:100%;table-layout:fixed;font-size:10pt;font-family:arial,sans,sans-serif;border-collapse:collapse;border:1px solid #ccc;font-weight:normal;color:black;background-color:white;text-align:center;text-decoration:none;font-style:normal;'
var htmltable = '<table ' + TABLEFORMAT +' ">';
for (row = 0; row<data.length; row++){
htmltable += '<tr>';
for (col = 0 ;col<data[row].length; col++){
  if (data[row][col] === "" || 0) {htmltable += '<td>' + 'None' + '</td>';} 
  else
    if (row === 0)  {
      htmltable += '<th>' + data[row][col] + '</th>';
    }
  else {htmltable += '<td>' + data[row][col] + '</td>';}
}
     htmltable += '</tr>';
}
     htmltable += '</table>';
     Logger.log(data);
     Logger.log(htmltable);
MailApp.sendEmail("myemail@gmail.com", 'Daily report','' ,{htmlBody: htmltable})
}

The problem is when i get mail the merged cells are not in merged state. 问题是当我收到邮件时,合并的单元格未处于合并状态。 This result in 3 columns instead of 1. Can someone please tweak the code so that I get exactly like i formatted sheets. 结果是3列而不是1列。有人可以调整代码,以便获得与格式化工作表完全相同的代码。

I modified the code a little because I didn't want to send any emails. 我做了一些修改,因为我不想发送任何电子邮件。 And if you'll move the table up to the top left corner and use sh.getDataRange() then you'll get the entire table with no problems. 而且,如果将表格移到左上角并使用sh.getDataRange(),那么整个表格都将毫无问题。

This is my modified version. 这是我的修改版本。

function sendSomething(){
 var sh = SpreadsheetApp.getActiveSheet();
 var dataA = sh.getDataRange().getValues();
 var None = '&nbsp;';
 var htmltable ='';
var TABLEFORMAT = 'cellspacing="2" cellpadding="2" dir="ltr" border="1" style="width:100%;table-layout:fixed;font-size:10pt;font-family:arial,sans,sans-serif;border-collapse:collapse;border:1px solid #ccc;font-weight:normal;color:black;background-color:white;text-align:center;text-decoration:none;font-style:normal;'
var htmltable = '<table ' + TABLEFORMAT +' ">';
for (row = 0; row<dataA.length; row++)
{
  htmltable += '<tr>';
  for(col = 0 ;col<dataA[row].length; col++)
  {
    if (dataA[row][col] === "" || 0) 
    {
      htmltable += '<td>' + None + '</td>';
    } 
    else
    if (row === 0)  
    {
      htmltable += '<th>' + dataA[row][col] + '</th>';
    }
    else 
    {
      htmltable += '<td>' + dataA[row][col] + '</td>';
    }
  }
     htmltable += '</tr>';
}
     htmltable += '</table>';
dispStatus('HTML',htmltable,800,500);
}

And this is my display routine which I incorporated into your code. 这是我的显示例程,已将其合并到您的代码中。 It's just my version of the logger. 这只是我的记录器版本。

function dispStatus(title,html,width,height,modal)
{
  var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
  var width = typeof(width) !== 'undefined' ? width : 400;
  var height = typeof(height) !== 'undefined' ? height : 300;
  var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
  var modal = typeof(modal) !== 'undefined' ? modal : false;
  var htmlOutput = HtmlService
     .createHtmlOutput(html)
     .setWidth(width)
     .setHeight(height);
 if(!modal)
 {
   SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
 }
 else
 {
   SpreadsheetApp.getUi().showModalDialog(htmlOutput, title);
 }
} 

You still need to format the dates. 您仍然需要格式化日期。 Perhaps you can use Utilities and if you want to merge cells together in the header you'll need to figure out which ones to give a colspan="3". 也许您可以使用实用程序,并且如果您想将标头中的单元格合并在一起,则需要弄清楚哪些单元格应提供colspan =“ 3”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Sheet Script,如何格式化电子邮件中的数据范围 - Google Sheet Script, How do I format the data range in my email 在Google表格上选择范围后,如何将选择内容粘贴到Google文档中? - After selecting a range on a Google Sheet, how do I paste the selection into a Google Doc? 进行编辑后,如何将范围从一个Google表格复制到另一个表格? - How do I copy a range from one Google Sheet to another when edits are made? 当特定单元格的值更改时,如何使Google表格脚本发送电子邮件? - How do I make a Google Sheet script send an email when a specific cell's value changes? 如何更改Google表格脚本中的设置范围,以便可以在单元格中选择它? - How do I change a set range in a google sheets script so that it can be selected in the cell? 如何获取Google表格单元格中的值并针对一系列单元格测试其内容? - How do I get the value in a Google Sheets cell and test its contents against a range of cells? 如何从Google表格中的某个范围创建javascript数组? - How can I create a javascript array froma range in a google sheet? Google Sheet 的正确格式和命令 - Json 数据表 - proper format and commands for Google Sheet - Json fed Datatables 如何使用JavaScript在Google Picker中下载Google表格? - How do I download a Google Sheet with Google Picker all in JavaScript? 如何通过 Google Sheet API 为 Google Sheet Cell 指定颜色 - How to specify colors for a Google Sheet Cell through the Google Sheet API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM