Retrieving Values from a Range in Apps Script
This post is a corrected post after AI explains the code included in the book “Google Apps Script 101: Building Work Automation For Free”. Added a strikethrough when editing what AI has written, and added color when edited by author
Table of Contents
Summary
In this blog post, we will explore how to retrieve values from a range in Apps Script. We will learn how to access a specific range in a spreadsheet, retrieve the values within that range, and log them using the Logger.
Code
1 2 3 4 5 6 |
function getA1C3Values() { const SS = SpreadsheetApp.getActiveSpreadsheet(); const T_SHEET2 = SS.getSheetByName('Sheet2'); const values = T_SHEET2.getRange('A1:C3').getValues(); Logger.log(values); } |
Code Explanation
The code defines a function named getA1C3Values
. It retrieves the active spreadsheet and assigns it to the SS
variable.
The code then gets the sheet named ‘Sheet2’ using the getSheetByName
method and assigns it to the T_SHEET2
variable.
The getRange
method is used to specify the range ‘A1:C3’ in the T_SHEET2
sheet. The getValues
method is called on the range to retrieve the values within that range.
The retrieved values are stored in the values
variable.
Finally, the Logger.log
method is used to log the retrieved values.
Example
When the getA1C3Values
function is executed, the values within the ‘A1:C3’ range in ‘Sheet2’ will be logged in the Apps Script Logger.
AI Prompt
Write a function that retrieves the values from the range ‘A1:C3’ in the sheet named ‘Sheet2’ of the active spreadsheet. Log the retrieved values using the Logger.