Here’s an example code snippet that accesses the active sheet in a Google Sheet using Apps Script:
function accessActiveSheet() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Perform operations on the active sheet var range = sheet.getRange('A1'); var value = range.getValue(); Logger.log('Value in cell A1: ' + value); }
In this code, the accessActiveSheet
function first uses SpreadsheetApp.getActiveSpreadsheet()
to retrieve the active spreadsheet. The getActiveSheet()
method is then called on the spreadsheet object to access the active sheet within the spreadsheet.
You can perform various operations on the sheet
object based on your requirements. In the example code, the getRange()
method is used to define a range (A1
in this case) on the active sheet. The getValue()
method retrieves the value within that range.
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 run this function by calling it from another function or setting up a trigger to execute it on a schedule or event. Upon execution, it will access the active sheet in the Google Sheet and perform the desired operations on it.
Feel free to modify the code to suit your specific requirements and add more functionality as needed.