Google 应用程序脚本。从 sheet 检索电子邮件列表

Google App Script. Retrieve emails list from the sheet

在下面的脚本中,我必须手动插入电子邮件地址。我宁愿从 sheet 中获取列表,以便能够在不编辑脚本的情况下更新它。 如何从 sheet (Sheet!A2:A) 检索电子邮件地址列表?

    function sendReport() {
  
  EMAIL = 'email'
  URL = 'url'
      + '/export?'
      + 'format=pdf'
      + '&size=A4' 
      + '&gid=id' 
      + '&scale=4'
      + '&portrait=true';

  
  SpreadsheetApp.getActive();

  var response = UrlFetchApp.fetch(URL, {
    headers: {
      'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
    }
  });

  var message = {
    to: EMAIL,
    subject: "subject",
    body: "body",
    attachments: [response.getBlob().setName('file.pdf')]
  }

  MailApp.sendEmail(message);
}

你期待的脚本是这样的吗?

修改后的脚本:

function sendReport() {
  URL = 'url'
    + '/export?'
    + 'format=pdf'
    + '&size=A4'
    + '&gid=id'
    + '&scale=4'
    + '&portrait=true';
  var response = UrlFetchApp.fetch(URL, {
    headers: {
      'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
    }
  });

  // Retrieve values from Spreadsheet.
  var sheet = SpreadsheetApp.getActiveSheet();
  var values = sheet.getRange("A2:A" + sheet.getLastRow()).getDisplayValues();
  
  // Send the email using the email addresses.
  values.forEach(([email]) => {
    if (email) {
      var message = {
        to: email,
        subject: "subject",
        body: "body",
        attachments: [response.getBlob().setName('file.pdf')]
      }
      MailApp.sendEmail(message);
    }
  });
}
  • 如果要设置具体的sheet,请将var sheet = SpreadsheetApp.getActiveSheet();修改为var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

参考: