Spreadsheets are a great archive for email snippets — you can skim a month of inboxes in one glance. How would you append each message as its own row?
Fetch the list from Gmail, loop through the messages, and call the Sheets append action once per message with the snippet as the row?
Exactly. One Gmail call, one Sheets call per message. Wrap the loop, count the rows you wrote, return the count so the caller knows how many landed:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
messages = emails.get("messages", [])
count = 0And inside the loop, each row is just a list with the snippet text? Same 2D shape Sheets expected yesterday?
Same 2D shape. Wrap the snippet in a row, call append, increment the counter. At the end, return the number of rows written:
def log_emails_to_sheet(max_results: int, spreadsheet_id: str) -> int:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
messages = emails.get("messages", [])
count = 0
for msg in messages:
text = msg.get("snippet", "")
toolset.execute_action(Action.GOOGLESHEETS_SPREADSHEETS_VALUES_APPEND, {
"spreadsheetId": spreadsheet_id, "range": "Sheet1",
"values": [[text]], "valueInputOption": "USER_ENTERED",
})
count += 1
return countOne API call per message — is that slow? What if I had 500 emails?
It would be slow — 500 round trips. In production you would batch them into one append call with all 500 rows in the values array. For this lesson, the one-call-per-row version makes the pattern obvious. The batched version is the same code with a different loop.
So by the end of the function, my spreadsheet has a brand-new row for every email and the function hands back the count?
Count plus the rows on the sheet. Open the tab after the test runs and you will see every snippet that came out of your inbox, stacked in order.
TL;DR: One fetch, one loop, N append calls — simple to read, scales poorly. For thousands of rows, batch the values array into a single call.
execute_action onceexecute_action per row inside the loop| Pattern | Round trips | When |
|---|---|---|
| N appends in a loop | N | small N, clear code |
| 1 append with N rows | 1 | large N, faster |
Spreadsheets are a great archive for email snippets — you can skim a month of inboxes in one glance. How would you append each message as its own row?
Fetch the list from Gmail, loop through the messages, and call the Sheets append action once per message with the snippet as the row?
Exactly. One Gmail call, one Sheets call per message. Wrap the loop, count the rows you wrote, return the count so the caller knows how many landed:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
messages = emails.get("messages", [])
count = 0And inside the loop, each row is just a list with the snippet text? Same 2D shape Sheets expected yesterday?
Same 2D shape. Wrap the snippet in a row, call append, increment the counter. At the end, return the number of rows written:
def log_emails_to_sheet(max_results: int, spreadsheet_id: str) -> int:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
messages = emails.get("messages", [])
count = 0
for msg in messages:
text = msg.get("snippet", "")
toolset.execute_action(Action.GOOGLESHEETS_SPREADSHEETS_VALUES_APPEND, {
"spreadsheetId": spreadsheet_id, "range": "Sheet1",
"values": [[text]], "valueInputOption": "USER_ENTERED",
})
count += 1
return countOne API call per message — is that slow? What if I had 500 emails?
It would be slow — 500 round trips. In production you would batch them into one append call with all 500 rows in the values array. For this lesson, the one-call-per-row version makes the pattern obvious. The batched version is the same code with a different loop.
So by the end of the function, my spreadsheet has a brand-new row for every email and the function hands back the count?
Count plus the rows on the sheet. Open the tab after the test runs and you will see every snippet that came out of your inbox, stacked in order.
TL;DR: One fetch, one loop, N append calls — simple to read, scales poorly. For thousands of rows, batch the values array into a single call.
execute_action onceexecute_action per row inside the loop| Pattern | Round trips | When |
|---|---|---|
| N appends in a loop | N | small N, clear code |
| 1 append with N rows | 1 | large N, faster |
Create a free account to get started. Paid plans unlock all tracks.