数组项ReferenceError问题:Google Apps Script列标题切换脚本报错
Fixing the "Today" ReferenceError & Column Header Toggle Script
Let's break down what's going wrong and fix your script step by step:
The Root Cause of the Error
The ReferenceError: "Today" is not defined happens because you're using Today as if it's a variable, but it's actually a text string in your cells. In JavaScript (which Google Apps Script uses), unquoted words are treated as variables—since you never declared a variable named Today, the script throws an error.
Other Issues in the Original Code
Besides the variable mistake, there are a couple more problems:
- You can't call
setValues()on the array returned bygetValues()—that method belongs to aRangeobject, not an array. - Directly comparing arrays with
==doesn't work as expected in JavaScript; we need to check the actual values inside the array. - Some syntax errors (missing quotes around empty spaces, inconsistent variable names like
newRoutinevsnewToday).
Corrected Script
Here's the fixed version that will toggle the "Today" header between the 3 columns:
function Progressions() { var workoutSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Workouts"); // Get the range containing the 3 headers (row 2, columns 1-3) var headerRange = workoutSheet.getRange(2, 1, 1, 3); // Get the values as a 2D array: [[col1Val, col2Val, col3Val]] var currentHeaders = headerRange.getValues()[0]; // Extract the first (and only) row as a 1D array var newHeaders; // Check where "Today" is currently located if (currentHeaders[0] === "Today") { // Move "Today" to the second column newHeaders = ["", "Today", ""]; } else if (currentHeaders[1] === "Today") { // Move "Today" to the third column newHeaders = ["", "", "Today"]; } else if (currentHeaders[2] === "Today") { // Move "Today" back to the first column newHeaders = ["Today", "", ""]; } // Update the range with the new headers if (newHeaders) { headerRange.setValues([newHeaders]); // Wrap the 1D array in another array for setValues() } }
Key Fixes & Improvements
- Quoted "Today": Now we use
"Today"(a string) instead ofToday(an undefined variable). - Range vs Array: We keep track of the original
headerRangeso we can callsetValues()on it later (instead of trying to call it on the array of values). - Simplified Value Checks: We extract the 1D array of header values and check the index of "Today" directly, making the logic easier to read.
- Proper Array Formatting:
setValues()expects a 2D array, so we wrap our 1DnewHeadersarray in another array when passing it to the method.
How to Test
- Replace your existing code with this version in the Apps Script editor.
- Make sure your "Workouts" sheet has row 2, columns A-C set up with one cell containing "Today" and the others empty (adjust the empty string checks if you use a different placeholder).
- Run the
Progressionsfunction—each run should move "Today" to the next column, looping back to the first after the third.
内容的提问来源于stack exchange,提问作者Dan Howard
相关产品推荐
相关产品推荐

