Google Script报错:调用setValues时出现'Incorrect range width'问题
Let’s break down what’s going on here and fix that frustrating error once and for all.
The Root Cause
That error message is spelling out the issue clearly: the range you’re targeting (A364:Z436) is 26 columns wide (from A to Z), but your GradeRows array has rows that are only 1 column wide. Google Sheets’ setValues() method requires the 2D array you pass to exactly match the dimensions of the range—same number of rows, same number of columns. If they don’t line up, you’ll get this mismatch error.
How to Fix It
You need to adjust your GradeRows array to ensure every sub-array (each row of data) has exactly 26 elements. Here are the most common scenarios and fixes:
1. Your data only fills the first column, others are empty
If you’re only populating column A and need columns B-Z to be blank, pad each row with empty strings to hit the 26-column requirement:
// Example: If you have a 1D array of values for column A var columnAData = ["student1", "student2", ..., "student73"]; // 73 total values // Convert to a 2D array with 26 columns per row var GradeRows = columnAData.map(function(studentName) { var row = new Array(26).fill(""); // Create an array of 26 empty strings row[0] = studentName; // Place the value in the first column (A) return row; });
2. You’re building rows incorrectly
Double-check how you’re constructing GradeRows—it must be a 2D array (an array of arrays). For example:
- ❌ Wrong: Each element is a single value, not an array
var GradeRows = ["val1", "val2", "val3"]; - ✅ Correct: Each element is an array with 26 elements (even if some are empty)
var GradeRows = [ ["val1", "", "", ..., ""], // 26 elements total ["val2", "", "", ..., ""], // ... 71 more rows ];
3. Verify array dimensions first
Add these logs right before your setValues() call to confirm everything matches up:
Logger.log("Number of rows in GradeRows: " + GradeRows.length); // Should be 73 if (GradeRows.length > 0) { Logger.log("Number of columns in first row: " + GradeRows[0].length); // Should be 26 }
If the column count isn’t 26, trace back to where you’re building GradeRows and fix the structure.
Modified Code Example
Here’s how your code should look after ensuring GradeRows is properly formatted:
var BeginningRow = LastSGRowSheet + 1; var EndingRow = BeginningRow + SGPushKtr - 1; Logger.log("BeginningRow =>" + BeginningRow + "<=, SGPushKtr =>" + SGPushKtr + "<=, Ending Row =>" + EndingRow + "<="); var GradesRangeString = 'A' + BeginningRow + ':' + LastStudentGradesColumnLetter + EndingRow; Logger.log("GradesRangeString =>" + GradesRangeString + "<="); // Validate array dimensions first to catch mismatches early Logger.log("GradeRows row count: " + GradeRows.length); if (GradeRows.length > 0) { Logger.log("GradeRows column count per row: " + GradeRows[0].length); } // Now write the data (dimensions should match!) StudentGradeSheet.getRange(GradesRangeString).setValues(GradeRows);
Final Note
This is one of the most common pitfalls with Google Apps Script’s sheet methods—always make sure your array’s rows and columns align perfectly with the range you’re targeting. Once you fix the GradeRows structure, that error should vanish.
内容的提问来源于stack exchange,提问作者David Kent




