To retrieve data from a specific range in a Google Sheet using Apps Script, you can use the getRange()
and getValues()
methods of the Sheet
class. Here’s an example code snippet:
function getDataFromRange() { var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var sheet = spreadsheet.getActiveSheet(); var range = sheet.getRange('A1:C3'); // Replace with the desired range var values = range.getValues(); // Process the retrieved data for (var i = 0; i < values.length; i++) { for (var j = 0; j < values[i].length; j++) { var value = values[i][j]; Logger.log('Cell (' + (i+1) + ', ' + (j+1) + '): ' + value); } } }
In this code, the getDataFromRange
function retrieves data from a specific range in the Google Sheet. The active spreadsheet is obtained using SpreadsheetApp.getActiveSpreadsheet()
. You can modify this line to access a specific spreadsheet if needed.
Next, the active sheet is obtained using getActiveSheet()
.
You can then define the range of cells you want to retrieve data from using the getRange()
method. In this example, the range is set to 'A1:C3'
, but you should change it to your desired range.
The getValues()
method is then called on the range
object. This method returns a two-dimensional array representing the values in the range.
The retrieved values are assigned to the values
variable.
To process the retrieved data, you can iterate over the values
array using nested for
loops. In this example, the data is logged using Logger.log()
, but you can perform any desired operations on the retrieved data.
You can call the getDataFromRange
function from another function or set up a trigger to execute it based on your needs. When executed, it will retrieve the values from the specified range in the Google Sheet and process them accordingly.
Feel free to modify the code to suit your specific requirements, such as retrieving data from different ranges, working with different data types, or performing additional operations on the retrieved data.