Get the value of a range by specifying the number of rows and columns in getRange
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 specific range in Google Sheets using Apps Script. We will use the getActiveSheet()
method to get the active sheet, getRange()
method to specify the range, and getValues()
method to retrieve the values within that range.
Code
1 2 3 4 |
function getD3E4Values() { const value = SpreadsheetApp.getActiveSheet().getRange(3, 4, 2, 2).getValues(); Logger.log(value); } |
Code Explanation
The code defines a function named getD3E4Values
. Within the function, we use the getActiveSheet()
method to get the currently active sheet. Then, we use the getRange()
method to specify the range starting from cell D3 (row 3, column 4) with a width of 2 rows and 2 columns. Finally, the getValues()
method retrieves the values within the specified range and assigns them to the value
variable.
The retrieved values are then logged using the Logger.log()
method.
Example
When the getD3E4Values()
function is executed, the values within the range D3:E4 will be logged in the Apps Script Logger.
AI Prompt
Write a function that retrieves the values from the range D3:E4 in the active sheet of a Google Sheets document and logs them using the Logger.log()
method.