Range Life Explainer

Aim of the puzzle: Use Apps Script to get the values of cells in the same column of a spreadsheet in Sheets.

Walk through of the solution: The sheet variable is storing a spreadsheet with 6 rows and 2 columns. In the code that you start off with, every value in column A is printed to the console.
To complete the puzzle, print the values of the cells in column B instead. First, after the columnA variable declaration but before the for…of loop, declare a new variable named columnB that stores the values of the range 'B1:B6' using .getRange().getValues(). Then, inside the setup of the outer for…of loop, change columnA to columnB.
Sample code solution:

let id = '1QE4pZs8efMoHKGOEHyXGT_nPZf1b_Sw7ssFCk2J2ueo';
let sheet =  SpreadsheetApp.openById(id);
 
let columnA = sheet.getRange('A1:A6').getValues();
    
let columnB = sheet.getRange('B1:B6').getValues();

for (let row of columnB) {
    for (let value of row) {
        console.log(value);
    }
}

JavaScript concepts: Variables, Strings, For…of Loops, Nested Arrays
Apps Script concepts: SpreadsheetApp, .openById(), .getRange().getValues()

1 Like