Code Coca

How to get sheet data in google app script

To get sheet data in Google Apps Script, you can use the getRange() method to select the range of cells you want to retrieve, and then use the getValues() method to get the data as a 2D array.

Here's an example of how to get sheet data:

App Script
function getSheetData() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
// Replace with your sheet name

var range = sheet.getRange("A1:C3"); // Replace with your desired range
var data = range.getValues(); // Do something with the data
}

In this example, we're selecting a range of cells (A1:C3) in the sheet named Sheet1, using the getRange() method. We then call the getValues() method on the range object to get the data as a 2D array.

You can then manipulate the data as needed. For example, you could loop through the rows and columns using nested for loops:

App Script
function getSheetData() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Replace with your sheet name
var range = sheet.getRange("A1:C3"); // Replace with your desired range
var data = range.getValues();
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
// Do something with the cell data, e.g. log it to the console
Logger.log(data[i][j]);
}
}
}

In this example, we're looping through each row and column in the data array using nested for loops, and logging the value of each cell to the console using Logger.log().

No comments:

Post a Comment