如何计算列中的值以使用它来定义变量以及如何使用 Google 应用程序脚本将 url 显示为超链接(文本)

How can I count the values in a column to use it to define a variable and how can I display a url as a hyperlink (text) using Google app script

我已经开发了以下脚本(借助 Whosebug 中的现有脚本)并且它完成了预期的工作。我想在脚本中更改两件事,但不确定如何更改。

1- 我不想读取我计算值的单元格中的值,而是想计算脚本中的值

2- 在我发出的电子邮件中,我想显示超链接文本而不是 url。

非常感谢任何帮助。

function sendEmails() {
  //enter the sheet name
  var sheetname = 'CFS Open Cases Report' 
  var counter_sheet = 'count of recipients'
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);

  //A1 is the cell that counts the number of rows in Column A. Try counting values in A2:A in the script instead
  var row_counter = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(counter_sheet).getRange("A2"); 
  var row_count = row_counter.getValue();
 

  var startRow = 2;  // First row of data to process
  var numRows = row_count;   // Number of rows to process

  //find a way to display the url as a hyperlink in the email body
  var report_url = "https://google.com";

 
  // Fetch the range of cells A2:D 
  var dataRange = sheet.getRange(startRow, 1, numRows, 4)
  // Fetch values for each row in the Range.

  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var first_name = row[0]; // First column
    var emailAddress = row[3]; // Fourth column
    
    //html message text
    var msgHtml = 'message and' + report_url
    ; 
    //today's date to be used as report date
    var report_date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
    var report_desc = "Report Name"
    var subject = report_date +' - '+ report_desc; 

            
    //send the email
    MailApp.sendEmail(emailAddress, subject, msgHtml);
  }
}

修改后的脚本如下:

function sendEmails() {
  var sheetname = 'Sheet1' // enter the sheet name where the recipient details are listed
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetname);
  var row_count = sheet.getRange("A2:A" + sheet.getLastRow()).getValues().flat().filter(String).length; // counts the rows in the A2:A range
  var startRow = 2; // first row of data to process
  var numRows = row_count; // number of rows to process
      
  var report_url = "www.google.com";    


  // Fetch the range of cells A2:D 
  var dataRange = sheet.getRange(startRow, 1, numRows, 4)
  // Fetch values for each row in the Range.

  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var first_name = row[0]; // First column
    var emailAddress = row[3]; // Fourth column
    
    //html message text
    var msgHtml = 'Hi ' + first_name +',' 
    + '<br/><br/>message here.'
    + '<br/><br/>more message here.'
    + '<br/><br/>and more: '+ '<a href="${report_url}">Go to Google</a>'
    + '<br/><br/>Kind Regards,'
    + '<br/><br/>my name'
    ; 
    //today's date to be used as report date
    var report_date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
    var report_desc = "CFS Open Cases Report"
    var subject = report_date +' - '+ report_desc; 

    // clear html tags and convert br to new lines for plain mail    
    var msgPlain = msgHtml.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, ""); 
    
    //send the email
    MailApp.sendEmail(emailAddress, subject, msgPlain);
  }
}

这就是我坚持的地方。我需要去掉 html 标签,但在发送电子邮件之前保留换行符:

var msgHtml = 'Hi ' + first_name +',' 
    + '<br/><br/>second line.'
    + '<br/><br/>third line.'
    + '<br/><br/>fourth line ' + `<a href="${report_url}">Go to Google</a>`
    + '<br/><br/>fifth line,'
    + '<br/><br/>sixth line'
    ;    

    //today's date to be used as report date
    var report_date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
    var report_desc = "CFS Open Cases Report"
    var subject = report_date +' - '+ report_desc; 

    // clear html tags and convert br to new lines for plain mail    
    var msgPlain = msgHtml.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, ""); 
    
    //send the email
    MailApp.sendEmail(emailAddress, subject, msgHtml); 
  }

终于明白哪里不对了:

MailApp.sendEmail(emailAddress, subject, msgHtml); 

将上面的内容更改为现在可以使用了:

MailApp.sendEmail({to: emailAddress, subject: subject, htmlBody: msgHtml});

修改点:

  • 关于Instead of reading the value in a cell where I do a count of values, I want to count the values in the script,为了将counta(A2:A)转换为Google Apps Script,我认为var row_count = counter_sheet_obj.getRange("A2:A" + counter_sheet_obj.getLastRow()).getValues().flat().filter(String).length;可能有用。

  • 关于In the email I send out, I want to display a hyperlink text instead of the url.,为了发送包含report_url超链接的邮件,我认为可以使用htmlBody。为此,我将 var msgHtml = 'message and' + report_url 修改为 var msgHtml = 'message and ' + ${report_url};.

当这些点体现在你的脚本中,就变成了下面这样。

修改后的脚本:

function sendEmails() {
  var sheetname = 'CFS Open Cases Report'
  var counter_sheet = 'count of recipients'
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetname);
  var counter_sheet_obj = ss.getSheetByName(counter_sheet);
  var row_count = counter_sheet_obj.getRange("A2:A" + counter_sheet_obj.getLastRow()).getValues().flat().filter(String).length;
  var startRow = 2;
  var numRows = row_count;
  var report_url = "https://google.com";
  var dataRange = sheet.getRange(startRow, 1, numRows, 4)
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var first_name = row[0]; // This is not used in your script.
    var emailAddress = row[3];
    var msgHtml = 'message and ' + `<a href="${report_url}">${report_url}</a>`;
    var report_date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
    var report_desc = "Report Name"
    var subject = report_date + ' - ' + report_desc;
    MailApp.sendEmail({to: emailAddress, subject: subject, htmlBody: msgHtml});
  }
}

参考:

已添加:

当我看到你现在的剧本时,我认为你没有正确反映我提议的剧本。请按如下方式修改您当前的脚本。

发件人:

+ '<br/><br/>and more: '+ '<a href="${report_url}">Go to Google</a>'

收件人:

+ '<br/><br/>and more: '+ `<a href="${report_url}">Go to Google</a>`
  • 请使用`代替'

参考: