MCPcopy Create free account
hub / github.com/analyticswithadam/App_Script / sheetArrayFunctions

Function sheetArrayFunctions

ArrayFunctions.js:23–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21
22// Function the Arrays of Arrays pulled from a Google Sheet
23function sheetArrayFunctions() {
24 let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Raw Data");
25 let data = sheet.getDataRange().getValues(); // dynamically detects the data range and returns the value
26 Logger.log(data);
27 let header = data.shift(); // Shift Removed the first element from an Array and Returns it
28 Logger.log(header);
29 Logger.log(data);
30
31 // Filter Function
32 let fruitsOnly = data.filter(
33 row => row[1] === "Fruit");
34 Logger.log(fruitsOnly);
35
36 // Map Function to each row
37 let updatedData = fruitsOnly.map(row => {
38 row[2] *= 1.10; // Increase price by 10%
39 return row;
40 });
41 Logger.log(updatedData);
42
43 // ForEach - Perform Operation on each row
44 updatedData.forEach(row => row[1] = "fruits");
45
46 // Insert header row back at the start
47 updatedData.unshift(header);
48
49 let ss = SpreadsheetApp.getActiveSpreadsheet();
50 let reportSheet = ss.getSheetByName("Fruit Report");
51 if (!reportSheet) {
52 reportSheet = ss.insertSheet("Fruit Report");
53 }
54
55 // Clear old data and write the new data
56 reportSheet.clear();
57 reportSheet.getRange(1, 1, updatedData.length, updatedData[0].length).setValues(updatedData);
58
59 Logger.log("Report generated successfully!");
60}
61
62/**
63 * Sample of Data

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected