Google表格脚本在getRange

时间:2019-08-22 13:46:33

标签: google-apps-script

POST更新: 嗨,最后,问题似乎与getvalue()本身无关,而与源有关,该源是由IMPORTRANGE公式导入的范围。这个公式很麻烦。我真的不知道为什么,但是我认为这与电子表格的大小有关。我无法检查这一点。我尝试下载文件失败。最后,我建立了一个新的电子表格。现在看来一切正常。但是,我仍然想了解下一次如何避免它的发生。 无论如何,谢谢您的帮助。

原始帖子:

我在GAS中遇到getRange函数的问题。这是一个非常简单的代码,可以正常工作,直到出现某些问题(我不知道是什么)出错然后开始失败为止。我只是想从一个单元格中收集一个日期值,然后用它来创建电子邮件。

我试图通过Logger和vars逐步调试代码,但是我很难弄清楚当我尝试从相应的单像元范围中获取值时会发生什么。

以下是我当前正在尝试的代码:

// Retreive email receipts from Form submitted choice

function EmailUpdatefromForm()
{

  var spreadsheet = SpreadsheetApp.openById("1ImOXXXXXxSDz8AqjMWtuSPtg7xMejqPpHQ9vwDnY"); 

  var form = FormApp.getActiveForm();
  var formResponses = form.getResponses();

  var formlength = formResponses.length;
  Logger.log(formlength);

    var formResponse = formResponses[formResponses.length-1];
    var itemResponses = formResponse.getItemResponses();

    var itemlength = itemResponses.length;
    Logger.log(itemlength);    

    for (var j = 0; j < itemResponses.length; j++) {
      var itemResponse = itemResponses[j];
      var emailto = itemResponse.getResponse();
      Logger.log(itemResponse+" - "+emailto);
    }

  var email = (emailto? emailto[0] : "xxxxxxxxxxs@xxxxxxx.com");
  var sheet = spreadsheet.getSheetByName("report");

  var log1 = spreadsheet.getId(); //--> THIS CHECKPOINT WORKS AND GETS THE PROPER SS ID
  var log2 = sheet.getSheetId(); //--> THIS CHECKPOINT WORKS AND GETS THE PROPER SHEET ID
  var log3 = sheet.getRange(2, 1) //--> THIS CHECKPOINT WORKS ALTHOUGH I CANNOT SEE IF IT IS ACTUALLY POINTING TO THE PROPER CELL

  var log4 = sheet.getRange(2, 1).getValues(); //--> THIS CHECKPOINT GETS STUCK\\


  var updatedate = sheet.getRange(2,1).getValue(); //--> THIS IS WHAT I REALLY WANT TO DO
  var PDFdate = Utilities.formatDate(updatedate, "GMT+1", "yyyyMMdd")

}

我发现有几篇文章提到了getRange的问题,但是我没有找到任何能真正说明这里发生什么情况以及如何解决它的答复。

提前感谢您的支持!


1 个答案:

答案 0 :(得分:0)

function EmailUpdatefromForm() {
  var spreadsheet = SpreadsheetApp.openById("1ImOXXXXXxSDz8AqjMWtuSPtg7xMejqPpHQ9vwDnY"); 
  var form = FormApp.getActiveForm();
  var formResponses = form.getResponses();
  var formlength = formResponses.length;
  Logger.log(formlength);
  var formResponse = formResponses[formResponses.length-1];
  var itemResponses = formResponse.getItemResponses();
  var itemlength = itemResponses.length;
  Logger.log(itemlength);    
  for (var j = 0; j < itemResponses.length; j++) {
    var itemResponse = itemResponses[j];
    var emailto = itemResponse.getResponse();
    Logger.log(itemResponse+" - "+emailto);
  }
  var email = (emailto? emailto[0] : "xxxxxxxxxxs@xxxxxxx.com");
  var sheet = spreadsheet.getSheetByName("report");
  var log1 = spreadsheet.getId();
  var log2 = sheet.getSheetId();
  var log3 = sheet.getRange(2, 1);
  var log4 = log3.getValue();
  var updatedate = log4;
  var PDFdate = Utilities.formatDate(updatedate, "GMT+1", "yyyyMMdd")
}