无法从Google表单电子表格中的某些行检索数据

时间:2012-06-07 10:39:28

标签: google-apps-script google-sheets

我尝试过这个脚本将行数据提取到数组中。 来自前两列的数据能够提取,但对于剩余的列,收集的数据显示为“未定义”。任何人都可以指导我在我的剧本中发生的错误吗?

在其他工作表中尝试相同,这很有效,但不是基于表单的电子表格? 任何人都可以解决这个脚本背后的错误

function sendEmails3() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Issues");
  var startRow = 2;  // First row of data to process
  var numRows  = sheet.getLastRow(); // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 3)

      // Fetch values for each row in the Range.
      var data = dataRange.getValues();
  var message_body2= new Array();
  var message_body1 =    " <html> <body> <table> <tbody>" 
      +"<tr>    <th> Timestamp </th> "
      +"    <th>    Program</th> "
      +"    <th>    District</th> "
      +"    <th>    Type to Issue</th> "
      +"    <th>    Name</th> "
      +"    <th>    Mobile Number</th> "
      +"    <th>    Personal Mail ID</th> "
      +"    <th>    Issue Description (not more that 100 words) if more, Kindly mail to ap_admin@drreddysfoundation.org</th> "
      +"    <th>    Share us the duration, since how many days the issue persist. ( in DAYS)</th> "
      +"    <th>    Center Mail ID</th> "
      +"    <th>    Issue Addressed</th> "
      +"    <th>    Issue Addressed Date</th> ";

  var message_body3 =   "</tbody> </table> </body> </html>";

  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var timeStamp = row[0];  // timestamp column
    var addressedDate = row[11];

    if (addressedDate != "" && timeStamp != "") {  // Prevents sending duplicates

      message_body2[i] = "</tr> <tr> "
        +"  <td>" + row[0] + "</td> "
        +"  <td> "+ row[1] + " </td> "
        +"  <td> "+ row[2] + " </td> "
        +"  <td> "+ row[3] + " </td> "
        +"  <td> "+ row[4] + " </td> "
        +"  <td> "+ row[5] + " </td> "
        +"  <td> "+ row[6] + " </td> "
        +"  <td> "+ row[7] + " </td> "
        +"  <td> "+ row[8] + " </td> "
        +"  <td> "+ row[9] + " </td> "
        +"  <td> "+ row[10]+ " </td> "
        +"  <td> "+ row[11]+ " </td> "
        +"  <td> "+ row[12]+ " </td> " + "</tr> " ; 
    }
    Browser.msgBox( row[2] + " - " + row[3] + " - " + row[4] + " - " +  row[5] + " - " + row[6] + " - " + row[7] + "  - " + row[8] + "  - " + row[9] + " - " + row[10]+ " - " + row[11]+ " - " + row[12]);
  }
  var complete_message = message_body1 + message_body2+ message_body3;

  // check data accumulated
  Browser.msgBox(complete_message);  

  // export to a cell all the extracted data to verifiy  
  sheet.getRange(i, 3).setValue(complete_message);
}

Click here to view the script & spreadsheet

1 个答案:

答案 0 :(得分:2)

看起来有几个不同的错误会导致您的问题。

首先,将startRow设置为2,但电子表格中的第2行为空白。您应该从第3行开始或删除第2行。

其次,这条线说只包括3列:

var dataRange = sheet.getRange(startRow, 1, numRows, 3)

最后一个参数3是数据范围中的列数。因此,如果您希望包含所有列,则它应为12.或者,您可以使用sheet.getLastColumn()。