0

我制作了一个小脚本,通过电子表格中的电子邮件发送文本。我使用 .getValue() 获得了单元格的文本,但是当我的单元格有两行或多行(Ctrl+enter)时,它只是将它们放在我在电子邮件中发送的表格中的同一行。

我想知道是否有办法在使用 .getValue() 提取 GoogheSheet 格式时保持它的格式,或者我是否需要重新处理字符串,如果是这样,我如何让任何 RE 检测到新行... ?

这是我用来提取单元格的编码:

function readInList(debR, col){
  /* Documentation
  La routine lit chaque cellule d'un range de 1 col par 5 row et stock les valeurs dans une liste au format texte.
  retourne cette liste
  */
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

  var stopRow = debR + 5;
  var opCol = col;
  var rangeList = [];
  var i;
  for (i=debR; i < stopRow; i++){
    var cell = sheet.getRange(i, col);
    rangeList.push(cell.getValue());
  }
  return (rangeList);
}

4

1 回答 1

0

所以 Mail 发送一个带有文本的重建表,所有这些都插入到一个 html Mail 中:

function weeklyReport() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var rowMonth = [31,28,31,30,31,30,31,30,31,30,31,30];
  
  
  
  var today = new Date();
  var todayYear = today.getYear();
  var todayMonth = today.getMonth();
  var todayDate = today.getDate();
  
  // Calc the number of rows depending of months that have passed
  var rowCell=0;
  var i = 0;
  do {
    rowCell+=rowMonth[i];
    i++;
  } while (i < todayMonth);
  rowCell+=3 + todayDate
  
  var range1 = readInListD(rowCell-4, 1);
  var range2 = readInList(rowCell-4, 4);
  
  var subject = 'Weekly report Clem Michard';
  var greetings = 'Hi, ';
  var sentence = 'please find my weekly report below:';
  var table = textToTable(range1, range2);
  var signature = 'Clem';
  var tables = today;
  var cmEmail= '***';
  var owaEmail = '***';
  var headEmail = '***';
  
  var htmlBodyA =
        "<html>"+
          "<head>"+
            "<style>"+
              "table, th, td {"+
                "border: 1px solid black;"+
                "border-collapse: collapse;"+
              "}"+
            "</style>" +
          "</head>"+
          "<body>"+
            "<p>" + greetings + "</p>"+
            "<p>" + sentence + "</p>"+
            "<pre>" + table + "</pre>"+
            "<p>" + signature + "</p>"+
          "</body>"+
        "</html>";    
          
        
    
/*- Message envoyé aux particpants avec un e-mail valide -*/               
  MailApp.sendEmail({
    to: headEmail,
    bcc: cmEmail,
    subject: subject,
    name: "Clément Michard",
    replyTo: cmEmail,
    htmlBody: htmlBodyA})
    
}

并使用它来组成表格:

function textToTable(col1, col2){
  /*
  This method converts a list of str into an HTML table
  
  takes 2 lists to build a table.
  */
  var listC1, listC2, text, lLen, i, j;

  listC1 = col1;
  listC2 = col2;
  lLen = listC1.length;
  text = "<table><tr><th>" + "Date" + "</th><th>" + "Report" + "</th></tr>";
  for (i = 0; i < lLen; i++){
    text += "<tr><td>" + listC1[i] + "</td><td>" + listC2[i] + "</td></tr>";
  }
  text += "</table>";
  
  return(text);
}

于 2018-05-11T07:09:20.960 回答