如何在电子邮件中粘贴带有Google脚本/ Google表格宏的表格?

时间:2019-02-26 20:22:23

标签: google-apps-script google-sheets

我有此Macro可以向某些人发送电子邮件。问题是它以值而不是以表格式粘贴表。我需要将表格粘贴为表格。

    function Mails() {
 var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
 var filas= values[0][1];
 var columnas= values[1][0];

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 ss.setActiveSheet(ss.getSheetByName("Enviar"));
 var sheet = SpreadsheetApp.getActiveSheet();
 var dataRange = sheet.getRange(2,4,filas,columnas);
 var data = dataRange.getValues();

 for (i in data) {
 var rowData = data[i];
 var emailAddress = sheet.getRange(1, 3).getValue();
 var destinatario = rowData[0];
 var sigla1 = sheet.getRange(2,4,filas,columnas).getValues();
 var mensaje = 'Estimado ' + destinatario + ',\n\n' + sigla1 ;
 var asunto = 'Mensajes Pendientes';
 MailApp.sendEmail(emailAddress, asunto, mensaje);
 }
}

function onOpen () {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Send Emails', functionName: 'Mails'}
];
spreadsheet.addMenu('Enviar Emails', menuItems);

}

问题是此行var sigla1 = sheet.getRange(2,4,filas,columnas).getValues(); 我必须将其粘贴为表格,而不是值。我该怎么办?

谢谢

我正在尝试使用此代码..但是它不起作用..

function Mails() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 ss.setActiveSheet(ss.getSheetByName("Enviar"));
 var sheet = SpreadsheetApp.getActiveSheet();

 var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
 var filas= values[0][1];
 var columnas= values[1][0];

 var recipient = sheet.getRange(1, 3).getValue();
 var subject = 'Estimado';

 var vA=SpreadsheetApp.getActive().getSheetByName('Enviar').getDataRange().getValues();
 var html="<style>th,td{border:1px solid black;}<table>";
 for(var i=0;i<vA.length;i++) {
    html+="<tr>";
    for(var j=0;i<vA[i].length;j++) {
      if(i==0) {
        html+=Utilities.formatString('<th>%s</th>', vA[i][j]);
      }else{
        html+=Utilities.formatString('<td>%s</td>', vA[i][j]);
      }
    }
    html+="</tr>";
  }

  GmailApp.sendEmail(recipient, subject, null, {htmlBody:html})

}

function onOpen () {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Send Emails', functionName: 'Mails'}
];
spreadsheet.addMenu('Enviar Emails', menuItems);

}

Here you have an image of what i need to send via email.

行随数据而变化,因此表取决于一天中的操作次数。

1 个答案:

答案 0 :(得分:0)

HTML表转换为电子邮件

function sendHtmlTableViaEmail() {
  var vA=SpreadsheetApp.getActive().getSheetByName('name').getDataRange().getValues();
  var html="<style>th,td{border:1px solid black;}<table>";
  for(var i=0;i<vA.length;i++) {
    html+="<tr>";
    for(var j=0;i<vA[i].length;j++) {
      if(i==0) {
        html+=Utilities.formatString('<th>%s</th>', vA[i][j]);
      }else{
        html+=Utilities.formatString('<td>%s</td>', vA[i][j]);
      }
    }
    html+="</tr>";
  }
 //You have to add the code for recipient and subject
  GmailApp.sendEmail(recipient, subject, null, {htmlBody:html})

}