To read the value from a specific cell in a Google Sheet using Apps Script, you can use the getValue()
method of the Range
class. Here’s an example code snippet:
function readCellValue() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var range = sheet.getRange('A1'); // Replace 'A1' with the desired cell reference var value = range.getValue(); Logger.log('Value in cell A1: ' + value); }
In this code, the readCellValue
function first retrieves the active sheet using SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
. You can modify this line to access a specific sheet if needed.
The getRange()
method is then called on the sheet object to define the range of the cell you want to read the value from. In this example, it’s set to 'A1'
, but you can change it to any desired cell reference, such as 'B5'
or 'C10'
.
Next, the getValue()
method is used on the range
object to retrieve the value from the specified cell.
The obtained value is then logged using Logger.log()
for demonstration purposes. You can view the log by going to View > Logs in the Apps Script editor.
You can call the readCellValue
function from another function or set up a trigger to execute it based on your needs. When executed, it will read the value from the specified cell and log it in the console.
Feel free to modify the code to suit your specific requirements and work with different ranges and cells.