【问题标题】:Google Apps Script getSheetByName returns nullGoogle Apps 脚本 getSheetByName 返回 null
【发布时间】:2020-10-12 20:05:01
【问题描述】:

getSheetNamegetSheetByName 的结果很奇怪

    function BE1() {
  console.log("BE1() triggered");
  var variables = globalVariables(); //load the Global variables 
  console.log("sheetid = ", variables.sheetid);
  var sheetName = 'BE1';
  var ss = SpreadsheetApp.openById(variables.sheetid)
  if (!ss) {
    console.log("spreadsheet not found");
  }  else  {
    var numSheets = ss.getNumSheets();
    console.log("spreadsheet has ", numSheets, " sheets");
    var someSheet = ss.getSheetName();
    console.log("someSheet = ", someSheet);
    if (sheetName === someSheet) {
      console.log("names match");
    }  else  {
      console.log("names do not match");
    }  
    var sheet = ss.getSheetByName(sheetName);
    if (!sheet) {
      console.log("sheet BE1 not found");
    }  else  {
      console.log("sheet = ", sheet);
      var lastrow = sheet.getLastRow(); 
      console.log("lastrow = ", lastrow);
      console.log("question column = ", variables.question);
      var nextQuestion = sheet.getRange(lastrow, variables.question).getValue(nextQuestion);
      SlidesApp.getUi().alert(nextQuestion);
    }
  }

控制台日志:

> Stackdriver logs
Oct 12, 2020, 2:40:11 PM    Debug   BE1() triggered
Oct 12, 2020, 2:40:11 PM    Debug   sheetid =  1QiuqmfF1z4Fe_RVD5BmPhvxq5sloHZs8ANFQ8gRLabA
Oct 12, 2020, 2:40:11 PM    Debug   spreadsheet has  2  sheets
Oct 12, 2020, 2:40:11 PM    Debug   someSheet =  BE1
Oct 12, 2020, 2:40:11 PM    Debug   names match
Oct 12, 2020, 2:40:11 PM    Debug   sheet =  {}
Oct 12, 2020, 2:40:11 PM    Debug   lastrow =  2
Oct 12, 2020, 2:40:11 PM    Debug   question column =  1
Oct 12, 2020, 2:40:11 PM    Error   Exception: The parameters (null) don't match the method signature for SpreadsheetApp.Range.getValue.
    at BE1(Code:45:70)

请注意,该字符串与ss.getSheetName() 检索到的任何工作表匹配,但不适用于getSheetByName。我犯了什么愚蠢的错误?为了显示到控制台,我已将所有代码分解为单独的行。

【问题讨论】:

  • 请注意,错误显示“参数(null)”而不是“找不到 null 方法 getRange()”。后者表示sheet 为空(当用作sheet.getRange() 时),而前者表示传递的参数(参数)为空。它并没有说工作表是空的。
  • 我赞成这个问题,因为 OP 试图 console.log 他/她认为可能导致错误的代码的每一部分。继续努力:)

标签: google-apps-script google-sheets


【解决方案1】:

解决方案:

替换

var nextQuestion = sheet.getRange(lastrow, variables.question).getValue(nextQuestion);

var nextQuestion = sheet.getRange(lastrow, variables.question).getValue();.


问题:


  • 另外nextQuestion在你分享的代码sn-p中也没有定义; 在使用之前。因此,nextQuestionnull,这是 你为什么得到:

    参数(null)与方法签名不匹配 SpreadsheetApp.Range.getValue。


最小可重现示例:

function myFunction() {

var ss = SpreadsheetApp.getActive();
var someSheet = ss.getActiveSheet();
var nextQuestion;
someSheet.getRange(1,1).getValue(nextQuestion);
}

nextQuestion 没有值。

这会给你同样的错误:

参数(null)与方法签名不匹配 SpreadsheetApp.Range.getValue。


奖金信息:

getSheetName() 应该与sheet 对象一起使用,而不是与spreadsheet 对象一起使用。

因此,不要使用:

var ss = SpreadsheetApp.openById(variables.sheetid)
var someSheet = ss.getSheetName();

最好改用这个逻辑:

var ss = SpreadsheetApp.getActive();
var someSheet = ss.getActiveSheet().getSheetName();

如果您想获得活动(选定)工作表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多