Aim of the puzzle: Use Apps Script to count how many times the same value appears in a spreadsheet from Sheets.
Walk through of the solution: In this puzzle, you’ll use Apps Script to count values in a spreadsheet and figure out how many customers are paying in stems.
To complete the puzzle, add an if statement inside the nested for…or loop that checks if cell === 'stems'
. Then, inside the code block {}
of the if statement, add stemCount++
.
Sample code solution:
let id = '1QE4pZs8efMoHKGOEHyXGT_nPZf1b_Sw7ssFCk2J2ueo';
let sheet = SpreadsheetApp.openById(id);
let values = sheet.getRange('B1:B8').getValues();
let stemCount = 0;
for (let row of values) {
for (let cell of row) {
if (cell === 'stems') {
stemCount++;
}
}
}
console.log(stemCount + ' customers are paying with stems.');
JavaScript concepts: Variables, Strings, Numbers, For…of Loops, Strict Equality Operator, Increment Operator
Apps Script concepts: SpreadsheetApp
, .openById()
, .getRange().getValues()