Office Scripts Lesson 2: TypeScript Basics for Excel Users

📎 This article includes 1 downloadable practice file ↓

⏱ 2 min read

📘 Office Scripts Beginner Course · Lesson 2 of 5

In this article
  1. Values and types
  2. Reading and writing a range
  3. Debugging
  4. Where people go wrong
  5. Practice

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

Leave a Reply

Your email address will not be published. Required fields are marked *