You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

数组项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 by getValues()—that method belongs to a Range object, 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 newRoutine vs newToday).

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 of Today (an undefined variable).
  • Range vs Array: We keep track of the original headerRange so we can call setValues() 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 1D newHeaders array in another array when passing it to the method.

How to Test

  1. Replace your existing code with this version in the Apps Script editor.
  2. 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).
  3. Run the Progressions function—each run should move "Today" to the next column, looping back to the first after the third.

内容的提问来源于stack exchange,提问作者Dan Howard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:15:09