【发布时间】:2020-10-12 20:05:01
【问题描述】:
getSheetName 和 getSheetByName 的结果很奇怪
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