
π This article includes 1 downloadable practice file β
In this article
You don’t need to be a programmer; a handful of TypeScript ideas covers most scripts.
Values and types
const rate = 0.18; // const: can't change
let total = 0; // let: can change
const name: string = "Sharma Traders";
Reading and writing a range
getValues() returns a 2-D array (rows of columns). Loop through it, then setValues() with an array of the same shape.
const data = sheet.getRange("A2:B6").getValues();
for (const row of data) {
const marks = row[1] as number;
const result = marks >= 40 ? "Pass" : "Fail";
}
Debugging
console.log(...) prints to the Output pane below the editor. Template strings: `Passed: ${passed}`.
π‘ Read all values once with getValues(), work in the array, and write once with setValues(). Touching cells one at a time in a loop is very slow in Office Scripts.
Where people go wrong
| Error | Cause |
|---|---|
| “Incorrect number of rows” | Array shape doesn’t match the range |
| Type errors on numbers | Cast with as number or Number() |
Practice
Put names in A2:A6 and marks in B2:B6, then run the script.
π Practice files for this article
- πLesson 2 Office ScriptPaste into Excel on the web > Automate > New Script.β¬ TS Β· 565 B
Free to use for learning. Files with macros (.bas) are plain text β import them with Alt+F11 β File β Import File, and always test on a copy.
β¨ Ask AI about this article
Stuck on a step? Ask a question and the AI answers using this article.
Free Β· AI can be wrong