使用getValue()的Google Sheet两行单元格

问题描述 投票:0回答:1

我制作了一个小脚本,通过电子表格中的电子邮件发送文本。我使用.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);
}
javascript google-sheets newline getvalue
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);
}
© www.soinside.com 2019 - 2024. All rights reserved.