Run your first Sheets script
Run your first bound Apps Script: read a small Customers sheet tab into the execution log and inspect the logged two-dimensional array.
Create a bound Sheets project, run `readAllCustomerRows`, and recognize the logged two-dimensional array.
Read a safe sample
Create a bound script that reads the Customers tab and writes its rows to the execution log without changing the spreadsheet.
function readAllCustomerRows() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Customers');
if (!sheet) throw new Error('Add a Sheet tab named Customers before running this function.');
const values = sheet.getDataRange().getValues();
Logger.log(values);
}
getDataRange() selects the used rectangle on the tab. getValues() returns that rectangle as an array of rows, and Logger.log() writes it to the execution log. The Range reference documents the read API and its Sheets authorization requirements; the Logger reference documents the log call.
Set up the spreadsheet
Create a new blank Google Sheet for this exercise. Do not use a live customer spreadsheet.
Create or rename its first tab to
Customers.Copy the comma-separated text from
sample.csvinto cellA1, or importsample.csvshown below with File > Import. Select column A, choose Data > Split text to columns, set the separator to Comma, and verify thatCustomers!A1:C4contains four rows starting with theName,Email, andStatusheaders:
Name,Email,Status
Avery Quinn,avery@example.com,Active
Morgan Lee,morgan@example.com,Paused
Riley Park,riley@example.com,Active
In that spreadsheet, choose Extensions > Apps Script. Replace the default code in
Code.gswith the completeCode.gsexample file.In Project Settings, select Show "appsscript.json" manifest file in editor, then replace its contents with the project manifest (
appsscript.json):
{
"timeZone": "Etc/UTC",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets.currentonly"
]
}
Save all files.
The project is bound to this spreadsheet, so getActiveSpreadsheet() returns the parent file. A standalone project would need to open a spreadsheet by ID or URL instead; see Google's bound-script guide.
Run the entrypoint
Choose
readAllCustomerRowsin the function menu at the top of the editor.Click Run.
Read the permission prompt. The manifest scope requests access only to the current spreadsheet.
Open Execution log after the run finishes.
The function runs as the user at the keyboard. Apps Script checks the scopes that code needs and prompts for consent when the user has not granted them. Google describes that lifecycle in its authorization guide.
Recognize the result
The log shows the rows from the sheet, including the header row:
[
["Name", "Email", "Status"],
["Avery Quinn", "avery@example.com", "Active"],
["Morgan Lee", "morgan@example.com", "Paused"],
["Riley Park", "riley@example.com", "Active"]
] The exact log display can differ, but it shows four rows and three values per row. This is a two-dimensional array: an outer array of rows, with an inner array of cell values for each row.
Handle ordinary failures
If the run stops with
Add a Sheet tab named Customers before running this function., rename or create the tabCustomers, then run again.If the authorization dialog requests broader access than the current spreadsheet, cancel it. Check that
appsscript.jsoncontains the current-spreadsheet scope and that no other service calls were added to the project.If the log has only one column, select column A and use Data > Split text to columns with Comma as the separator so the input spans four rows by three columns.
Change the input
Change Paused in the sample sheet to Active and run readAllCustomerRows again. The logged value for Morgan changes from Paused to Active.
Clean up and continue
Delete the disposable spreadsheet when you no longer need it, which also removes its bound script. Next, store and compare values in JavaScript so your script can inspect each row and decide what to do with it.
Project files
Complete source files for this chapter’s examples.
Read all customer rows
Code.gs
Download filefunction readAllCustomerRows() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Customers');
if (!sheet) throw new Error('Add a Sheet tab named Customers before running this function.');
const values = sheet.getDataRange().getValues();
Logger.log(values);
}
appsscript.json
Download file{
"timeZone": "Etc/UTC",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets.currentonly"
]
}
sample.csv
Download fileName,Email,Status
Avery Quinn,avery@example.com,Active
Morgan Lee,morgan@example.com,Paused
Riley Park,riley@example.com,Active