在Google Apps Script中设置calendarID变量遇阻(表格同步GCal场景)
Let's break down the two key issues in your code and fix them right away:
1. Wrong Column Index for Cell G2
Google Apps Script uses 1-based indexing for columns in getRange(row, column). Since column A is 1, column G is actually the 7th column—not 6. Your current getRange(2,6) is targeting cell F2 instead of G2.
2. Incorrect Way to Call getValue()
You already stored the range object in the range variable, so you don't need to reference it through the sheet object again. sheet.range is invalid because the Sheet class doesn't have a property named range—this will throw a runtime error.
Corrected Code
var sheet = SpreadsheetApp.getActiveSheet(); // Target G2: row 2, column 7 var range = sheet.getRange(2, 7); // Call getValue() directly on the range variable var calendarId = range.getValue();
Quick Verification Tip
To double-check you're grabbing the right cell, you can add a log statement to confirm the value:
Logger.log("Calendar ID retrieved: " + calendarId);
Then check the logs via View > Logs in the Apps Script editor to ensure it matches the ID in G2.
内容的提问来源于stack exchange,提问作者Thomas

