Google Spreadsheets使用多个标签更新A脚本

时间:2017-06-25 10:10:00

标签: google-apps-script

我找到了一些如何更新脚本的线程,但是我无法使用脚本中的限制知识

我有大约6个Tabs,Sheet1,Sheet2,Sheet3,Sheet4,Sheet5,Sheet6,我想发送电子邮件,但在这个阶段它只查看第一张表。有人可以告诉我如何更新这个

感谢Jarrad

function sendEmails() {
   var sheet = SpreadsheetApp.getActiveSheet();
   var range = sheet.getRange(1, 2);  // Fetch the range of cells B1:B1
   var subject = range.getValues();   // Fetch value for subject line from above range
   var range = sheet.getRange(1, 9);  // Fetch the range of cells I1:I1
   var numRows = range.getValues();   // Fetch value for number of emails from above range
   var startRow = 4;                  // First row of data to process
   var dataRange = sheet.getRange(startRow, 1, numRows,9 ) // Fetch the range of cells A4:I_
   var data = dataRange.getValues();  // Fetch values for each row in the Range.
   for (i in data) {
      var row = data[i];
      var emailAddress = row[1];      // Second column
      var message = row[8];           // Ninth column
      MailApp.sendEmail(emailAddress, subject, message);
   }
}

嗨Cooper 感谢您的信息,但我只是刚刚开始使用脚本我想从现在的基础开始,并在那里生病。我不能想象你说我知道它的简单信息,但我还是不能解决它。 我是否删除此行var sheet = SpreadsheetApp.getActiveSheet();并替换为。我在行var range行收到错误。我需要完整地看到它,试着去理解我在看什么。对不起基本问题

var sheets=['Sheet1','Sheet2','Sheet3','Sheet4']
  for(var i=0;i<sheets.length;i++)



function sendEmails() {
   var sheet = SpreadsheetApp.getActiveSheet();
   var range = sheet.getRange(1, 2);  // Fetch the range of cells B1:B1
   var subject = range.getValues();   // Fetch value for subject line from above range
   var range = sheet.getRange(1, 9);  // Fetch the range of cells I1:I1
   var numRows = range.getValues();   // Fetch value for number of emails from above range
   var startRow = 4;                  // First row of data to process
   var dataRange = sheet.getRange(startRow, 1, numRows,9 ) // Fetch the range of cells A4:I_
   var data = dataRange.getValues();  // Fetch values for each row in the Range.
   for (i in data) {
      var row = data[i];
      var emailAddress = row[1];      // Second column
      var message = row[8];           // Ninth column
      MailApp.sendEmail(emailAddress, subject, message);

2 个答案:

答案 0 :(得分:1)

完成电子邮件发送和存档示例

这是我刚刚做的一个完整的例子。我认为是时候向我学习一些关于从电子表格发送电子邮件的知识,因为我们经常收到这样的问题。现在在你的情况下你想要为不同的表格做,所以我在sendingMyEmails()函数中添加了一个参数。所以现在它正在发送MyEmails(sheetname)。您必须为每个工作表添加单独的函数调用,如下所示:

function sendEmailsSheet1()
{
   sendingMyEmails('Sheet1');
} 

或者你可以这样做:

function sendAllSheets()
{
  var sheets=['Sheet1','Sheet2','Sheet3','Sheet4']
  for(var i=0;i<sheets.length;i++)
  {
     sendMyEmails(sheets[i]);
  }
}

无论哪种方式,您只需要一个功能来发送每张表中的所有电子邮件。此示例还包括将电子邮件存档到emailSent选项卡的功能。我会收集所有这些信息,然后将其显示为HTML对话框,以便您查看要存档的电子邮件。

SendingEmails.gs

function sendingMyEmails(sheetname) 
{
    var ss=SpreadsheetApp.getActiveSpreadsheet();
    var sht=ss.getSheetByName(sheetname);
    var rng=sht.getDataRange();
    var rngA=rng.getValues();
    var s='<html><head><script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script></head><body>';
    var someNotSent=false;
    for(var i=2;i<rngA.length;i++)
    {
      var dataA={};
      for(var j=0;j<rngA[1].length;j++)
      {
        dataA[rngA[1][j]]=rngA[i][j];
      }
      if(dataA.EntryDate && dataA.Name && dataA.Email && dataA.Subject && dataA.Body && dataA.SendIfYes=='Yes' && dataA.DateSent=='')
      {
        MailApp.sendEmail(dataA.Email, dataA.Subject, dataA.Body, {replyTo:dataA.Sender});
        sht.getRange(i+1,8).setValue(Utilities.formatDate(new Date(), 'GMT-6', "M/dd/yyyy'\n'HH:mm:ss"));
        SpreadsheetApp.flush();
      }
      else
      {
        var row=Number(i+1);
        s+='<div  id="row' + row + '"><input type="checkbox" name="email" value="' + Number(i+1) + '" />' + 'Email Not Sent: <strong>Row:</strong> ' + Number(i+1) + ' <strong>Name:</strong> ' + dataA.Name + ' <strong>Email:</strong> ' + dataA.Email + ' <strong>Subject:</strong> ' + dataA.Subject + ' <strong>EntryDate:</strong> ' + dataA.EntryDate + '</div>';
        someNotSent=true;
      }
    }
    if(someNotSent)
    {
      s+='<br /><input type="button" value="Exit" onClick="google.script.host.close();" /><input type="button" value="Archive Checked" onClick="getCheckedBoxes(\'email\');" />';
      s+='</body></html>';
      var html=HtmlService.createHtmlOutputFromFile('htmlToBody').setWidth(800).setHeight(250);
      html.append(s);
      SpreadsheetApp.getUi().showModelessDialog(html, 'Emails Not Sent');
    }
}

function archiveSelectedEmails()
{
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sht=ss.getSheetByName('EmailSetup');
  var rng=sht.getDataRange();
  var rngA=rng.getValues();
  var s='<html><head><script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script></head><body>';
  var s='';
  for(var i=2;i<rngA.length;i++)
  {
    var dataA={};
    for(var j=0;j<rngA[1].length;j++)
    {
      dataA[rngA[1][j]]=rngA[i][j];
    }
    var row=Number(i+1);
    s+='<div id="row' + row + '"><input type="checkbox" name="email" value="' + Number(i+1) + '" />' + ' <strong>Row:</strong> ' + Number(i+1) + ' <strong>Name:</strong> ' + dataA.Name + ' <strong>Email:</strong> ' + dataA.Email + ' <strong>Subject:</strong> ' + dataA.Subject + ' <strong>DateSent:</strong> ' + Utilities.formatDate(new Date(dataA.DateSent), 'GMT-6', "M/dd/yyyy HH:mm:ss") + '</div>';
  }
  s+='<br /><input type="button" value="Exit" onClick="google.script.host.close();" /><input type="button" value="Archive Checked" onClick="getCheckedBoxes(\'email\');" />';

  var html=HtmlService.createHtmlOutputFromFile('htmlToBody').setWidth(800).setHeight(250);
  html.append(s);
  SpreadsheetApp.getUi().showModelessDialog(html, 'Select Emails to Archive');
}

function test()
{
  var rows=[3];
  archiveSelectedRows(rows);
}

function archiveSelectedRows(rows,sheetname)
{
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sht=ss.getSheetByName(sheetname);
  var dest=ss.getSheetByName('EmailsSent');
  var rng=sht.getDataRange();
  var rngA=rng.getValues();
  var deleted=[];
  for(var i=rngA.length-1;i>1;i--)
  {
    if(rows.indexOf(i+1)>-1)
    {
      deleted.push(Number(i+1));
      dest.appendRow(rngA[i]);
      sht.deleteRow(i+1);
    }
  }
  var msg='Rows Deleted = ' + deleted;
  var title='Rows Deleted';
  var timeout=10;
  return deleted;
}

样本表

这是我的emailSetup表的标题行的图像,而且emailsS​​ent表具有相同的列。我没有使用对话框来识别已发送的电子邮件,以便我可以将它们存档在另一张表格中。 enter image description here

htmlToBody.html

<!DOCTYPE html>
<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
function getCheckedBoxes(chkboxName) {
  var checkboxes = document.getElementsByName(chkboxName);
  var rowsToArchive = [];
  for (var i=0; i<checkboxes.length; i++) 
  {
     if (checkboxes[i].checked) 
     {
        rowsToArchive.push(Number(checkboxes[i].value));
     }
  }
  google.script.run
    .withSuccessHandler(setResponse)
    .archiveSelectedRows(rowsToArchive);
}

function setResponse(a)
{
  var s='<br />Rows: ';
  for(var i=0;i<a.length;i++)
  {
    if(i>0)
    {
      s+=', ';
    }
    s+=a[i];
    var id='#row' + a[i]
    $(id).css('display','none');
  }
  s+='<br />Total: ' + a.length;
  google.script.run.displayMessage(s,'Archived Rows')
}
console.log('script here');
</script>
   </head>  
  <body>

Utility.gs

function displayToast(msg,title,timeoutSeconds)
{
  SpreadsheetApp.getActiveSpreadsheet().toast(msg, title, timeoutSeconds)
}

function displayMessage(msg,title)
{
  msg+='<br /><input type="button" value="Exit" onClick="google.script.host.close()"; />';
  var html=HtmlService.createHtmlOutput(msg).setWidth(400).setHeight(300);
  SpreadsheetApp.getUi().showModelessDialog(html, title);
}

Code.gs

function onOpen()
{
  SpreadsheetApp.getUi().createMenu('My Tools')
  .addItem('Send Emails','sendingMyEmails')
  .addItem('Archive Selected Emails','archiveSelectedEmails')
  .addToUi();

}

答案 1 :(得分:1)

你可能想要使用这样的东西

function sendEmails()
{
  var mySheets=['Telstra out of date','Optus out of date','Vodafone out of date','Drivers Licence out of date'];
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var allSheets=ss.getSheets();
  for(var i=0;i<allSheets.length;i++)
  { 
    var sht=allSheets[i]; 
    if(mySheets.indexOf(sht.getName())>-1)//if you have sheets you don't want to send emails from
    {
      var rng=sht.getDataRange();
      var rngA=rng.getValues();
      var subject = rngA[0][1];//B1
      for(var j=3;j<rngA.length;j++)//start row was 4
      {
        var emailAddress = rngA[j][1];//col 2
        var message = String(rngA[3][2]) + String(rngA[j][3]);//col 3
        if(emailAddress)
        {
          MailApp.sendEmail(emailAddress, subject, message);
          //Logger.log('EmailAddress: ' + emailAddress + ' Subject: ' + subject + ' Message: ' + message);
        }
      }
    }
  }
}