我发送电子邮件与谷歌张采用一个模板

0

的问题

我想跑下脚本发送电子邮件的模板在片[1]A1。 每一次脚本触发它选择的数据fillInTheTemplate功能的范围

const grg = sheet.getRange(2, 1, 6, 36); 我要求代码只选择的范围fillTemplateFunction从行其已没有"Email_Sent"在排36

谢谢你提供的任何援助

 * Sends emails from spreadsheet rows.
 */
function sendEmails() {
  const ss = SpreadsheetApp.getActive();
  const dsh = ss.getSheets()[0];//repl with getshbyname
  const drg = dsh.getRange(2, 1, dsh.getLastRow() - 2, 36);
  const vs = drg.getValues();
  const tsh = ss.getSheets()[1];//repl with getshbyname
  const tmpl = tsh.getRange('A1').getValue();
  var sheet = SpreadsheetApp.getActiveSheet();
  const grg = sheet.getRange(2, 1, 6, 36);
  objects = getRowsData(sheet, grg);
  for (var i = 0; i < objects.length; ++i) {
  var rowData = objects[i];}
  vs.forEach((r,i) => {
    let emailSent = r[35]; 
    let status = r[10];  
    if (status == 'PRICE ONLY' && emailSent != 'EMAIL_SENT') { 
    MailApp.sendEmail(r[9], 'SUPERMIX QUOTATION',fillInTemplateFromObject(tmpl, rowData) );//if last paramenter is the options object then you are missing the  null for the body. but since fillInTemplateFromObject is undefined I can not know that
    dsh.getRange(2 + i, 36).setValue('EMAIL_SENT');
    }
  });
}

/**
 * Replaces markers in a template string with values define in a JavaScript data object.
 * @param {string} template Contains markers, for instance ${"Column name"}
 * @param {object} data values to that will replace markers.
 *   For instance data.columnName will replace marker ${"Column name"}
 * @return {string} A string without markers. If no data is found to replace a marker,
 *   it is simply removed.
 */
function fillInTemplateFromObject(tmpl, grg) {
  console.log('[START] fillInTemplateFromObject()');
  var email = tmpl;
  // Search for all the variables to be replaced, for instance ${"Column name"}
  var templateVars = tmpl.match(/\$\{\"[^\"]+\"\}/g);

  // Replace variables from the template with the actual values from the data object.
  // If no value is available, replace with the empty string.
  for (var i = 0; templateVars && i < templateVars.length; ++i) {
    // normalizeHeader ignores ${"} so we can call it directly here.
    var variableData = grg[normalizeHeader(templateVars[i])];
    email = email.replace(templateVars[i], variableData || '');
  }
SpreadsheetApp.flush();
  return email;
}
/**
 * Iterates row by row in the input range and returns an array of objects.
 * Each object contains all the data for a given row, indexed by its normalized column name.
 * @param {Sheet} sheet The sheet object that contains the data to be processed
 * @param {Range} range The exact range of cells where the data is stored
 * @param {number} columnHeadersRowIndex Specifies the row number where the column names are stored.
 *   This argument is optional and it defaults to the row immediately above range;
 * @return {object[]} An array of objects.
 */
function getRowsData(sheet, range, columnHeadersRowIndex) {
  columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
  var numColumns = range.getEndColumn() - range.getColumn() + 1;
  var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
  var headers = headersRange.getValues()[0];
  return getObjects(range.getValues(), normalizeHeaders(headers));
}

/**
 * For every row of data in data, generates an object that contains the data. Names of
 * object fields are defined in keys.
 * @param {object} data JavaScript 2d array
 * @param {object} keys Array of Strings that define the property names for the objects to create
 * @return {object[]} A list of objects.
 */
function getObjects(data, keys) {
  var objects = [];
  for (var i = 0; i < data.length; ++i) {
    var object = {};
    var hasData = false;
    for (var j = 0; j < data[i].length; ++j) {
      var cellData = data[i][j];
      if (isCellEmpty(cellData)) {
        continue;
      }
      object[keys[j]] = cellData;
      hasData = true;
    }
    if (hasData) {
      objects.push(object);
    }
  }
  return objects;
}

/**
 * Returns an array of normalized Strings.
 * @param {string[]} headers Array of strings to normalize
 * @return {string[]} An array of normalized strings.
 */
function normalizeHeaders(headers) {
  var keys = [];
  for (var i = 0; i < headers.length; ++i) {
    var key = normalizeHeader(headers[i]);
    if (key.length > 0) {
      keys.push(key);
    }
  }
  return keys;
}

/**
 * Normalizes a string, by removing all alphanumeric characters and using mixed case
 * to separate words. The output will always start with a lower case letter.
 * This function is designed to produce JavaScript object property names.
 * @param {string} header The header to normalize.
 * @return {string} The normalized header.
 * @example "First Name" -> "firstName"
 * @example "Market Cap (millions) -> "marketCapMillions
 * @example "1 number at the beginning is ignored" -> "numberAtTheBeginningIsIgnored"
 */
function normalizeHeader(header) {
  var key = '';
  var upperCase = false;
  for (var i = 0; i < header.length; ++i) {
    var letter = header[i];
    if (letter == ' ' && key.length > 0) {
      upperCase = true;
      continue;
    }
    if (!isAlnum(letter)) {
      continue;
    }
    if (key.length == 0 && isDigit(letter)) {
      continue; // first character must be a letter
    }
    if (upperCase) {
      upperCase = false;
      key += letter.toUpperCase();
    } else {
      key += letter.toLowerCase();
    }
  }
  return key;
}

/**
 * Returns true if the cell where cellData was read from is empty.
 * @param {string} cellData Cell data
 * @return {boolean} True if the cell is empty.
 */
function isCellEmpty(cellData) {
  return typeof(cellData) == 'string' && cellData == '';
}

/**
 * Returns true if the character char is alphabetical, false otherwise.
 * @param {string} char The character.
 * @return {boolean} True if the char is a number.
 */
function isAlnum(char) {
  return char >= 'A' && char <= 'Z' ||
    char >= 'a' && char <= 'z' ||
    isDigit(char);
}

/**
 * Returns true if the character char is a digit, false otherwise.
 * @param {string} char The character.
 * @return {boolean} True if the char is a digit.
 */
function isDigit(char) {
  return char >= '0' && char <= '9';
}```


google-apps-script google-sheets
2021-11-23 23:09:33
1

最好的答案

1

当我看到你的脚本,看来你的脚本发送电子邮件通过检查柱"K"(列数是11)栏中的"J"(列数字是36) if (status == 'PRICE ONLY' && emailSent != 'EMAIL_SENT') {}. 而且,当我看到你的电子表格样本,发行数量没有价值的"EMAIL_SENT"在所列的"J"是5。 但是,当我看到列"K",没有任何价值是存在的。 通过这个, status == 'PRICE ONLY' && emailSent != 'EMAIL_SENT' 返回 false. 通过此,电子邮件是不能发送。 我认为,这可能是因为你的问题。

如果你想发送电子邮件的时候所列"A"没有价值的"EMAIL_SENT",如何关于以下修改?

自:

if (status == 'PRICE ONLY' && emailSent != 'EMAIL_SENT') { 

为:

if (emailSent != 'EMAIL_SENT') {

添加:

从下面的回答,

代码发送电子邮件作为我需要和'Email_Sent'是放在列AJ,因为它应该。 我的问题涉及与功能fillInTemplateFromObject(tmpl,纳哈广电)的功能。 的范围内,我使用这个功能是'const纳哈广电=片。getRange(2, 1, 5, 36); ' 这开始于行2行和包括5个行。 当我发送的电子邮件的元数据的范围内为该模板数据是从第6行,即使行其Email_Sent是行4. 该模板应该采取的数据相同的细胞其电子邮件发送。

在这种情况下,如何对以下修改?

自:

  for (var i = 0; i < objects.length; ++i) {
  var rowData = objects[i];}
  vs.forEach((r,i) => {
    let emailSent = r[35]; 
    let status = r[10];  
    if (status == 'PRICE ONLY' && emailSent != 'EMAIL_SENT') { 
    MailApp.sendEmail(r[9], 'SUPERMIX QUOTATION',fillInTemplateFromObject(tmpl, rowData) );//if last paramenter is the options object then you are missing the  null for the body. but since fillInTemplateFromObject is undefined I can not know that
    dsh.getRange(2 + i, 36).setValue('EMAIL_SENT');
    }
  });

为:

vs.forEach((r, i) => {
  let emailSent = r[35];
  if (emailSent != 'EMAIL_SENT') {
    MailAppaa.sendEmail(r[9], 'SUPERMIX QUOTATION', fillInTemplateFromObject(tmpl, objects[i]));
    dsh.getRange(2 + i, 36).setValue('EMAIL_SENT');
  }
});
2021-11-25 23:51:20

抱歉,我试着要清楚说明问题。 代码发送电子邮件作为我需要和'Email_Sent'是放在列AJ,因为它应该。 我的问题涉及与功能fillInTemplateFromObject(tmpl,纳哈广电)的功能。 的范围内,我使用这个功能是'const纳哈广电=片。getRange(2, 1, 5, 36); ' 这开始于行2行和包括5个行。 当我发送的电子邮件的元数据的范围内为该模板数据是从第6行,即使行其Email_Sent是行4. 该模板应该采取的数据相同的细胞其电子邮件发送。
Les

我不知道该怎么告诉的fillInTemplatefromObject的功能选择的细胞同样的行其电子邮件发送。 我希望你会明白什么我要找到实现。 谢谢你的帮助。
Les

@莱谢谢你的回复。 我道歉,我的可怜的英文技能。 从你的回答,我增加了一个修改一点在我的答案。 能不能请你确认? 如果我误会了您的问题再一次,我再次道歉.
Tanaike

是的太好了,这完美的作品。 非常感谢Tanaike
Les

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................