Dataset to Google Sheets (service account) avatar

Dataset to Google Sheets (service account)

Pricing

from $0.20 / 1,000 row written or updateds

Go to Apify Store
Dataset to Google Sheets (service account)

Dataset to Google Sheets (service account)

One writer per tab. While this Actor writes to a tab, nothing else may write to that tab: no second run, no other tool, no person. Use a tab that belongs to this export. Writes an Apify dataset to a Google Sheet tab with your own service account: append, replace or upsert.

Pricing

from $0.20 / 1,000 row written or updateds

Rating

0.0

(0)

Developer

Bardach Technologies

Bardach Technologies

Maintained by Community

Actor stats

0

Bookmarked

2

Total users

1

Monthly active users

6 hours ago

Last modified

Features

Limited permissions

Share

One writer per tab. While this Actor writes to a tab, nothing else may write to that tab: no second run, no other tool, no person. Use a tab that belongs to this export.

Write one of your Apify datasets into a tab of a Google Sheet. You share the sheet with your own Google service account and give the Actor that account's key. There is no Google sign-in screen and no token that expires, so the Actor works from schedules, webhooks and the API.

You choose what happens to the tab: append rows below the existing ones, replace the content, or upsert (update the row whose key matches, add the rest). New fields in the dataset become new columns on the right; existing columns never move.

What this Actor enforces, detects, and cannot prevent

The contract above is the whole promise. The Actor does three things about it, and one thing it cannot do.

  • It enforces it against a second run of this Actor. Before it reads or writes the tab, it takes a lock for that tab (a small record on the spreadsheet, see "The tab lock"). A second run finds the lock and, by default, stops at once without writing or charging anything. Set ifTabBusy to wait to make it wait up to waitMinutes minutes for the first run to finish.
  • It detects other writers. Before every write attempt, including each retry after a 429 or 5xx wait, it reads the rows it is about to write. If they hold something it did not write, it does not write there: it moves the batch below the used range. After each write it reads the rows back. A row is confirmed, and charged, only if it reads back exactly as sent. If a row does not, or a header cell it was adding was changed, the run stops with status incomplete and a message that names the rows. It never carries on as if the export were complete, and it does not charge for the rows it could not confirm.
  • It never reports success for rows that are not in the sheet. Charging happens after the read-back, never before.
  • It does not write on a lock it may have lost. Immediately before every write request, each retry included, the run reads the lock record again and requires its own run ID and token and a heartbeat well inside the stale limit (at most 300 s old), then writes a new heartbeat. A request is handed to the network only within 240 seconds of that check (the stale limit after which another run may take over is 600 seconds); if the token refresh, the pacing wait or anything else holds it up longer, the request is not sent and the check is made again. If the lock is no longer this run's, or Google does not let the run confirm it, the run does not write: it stops as incomplete, names the rows it did not write, and charges nothing for them. After the read-back the record is read once more, and a run that was superseded in the meantime reports incomplete and charges nothing for that batch instead of complete. The Sheets API has no "write only if", so this is a check made at the last moment, not a guarantee: a request already on the wire cannot be recalled, but every Google request has a total deadline of 90 seconds of wall-clock time (connect, sending and the whole answer together, not a wait per network step): when it passes, the connection is closed and the answer counts as lost, so the run looks at the rows again before it sends anything. Limit: the name lookup and the connect (a connect that tries several addresses one after the other) are not cut by the timer; the system bounds them separately, and a stalled one can take longer than 90 seconds. Nothing is sent once the 90 seconds have passed, so the figure below still holds. 240 + 90 + 2 minutes of allowed clock difference between two runs = 450 s, under 600 s.
  • What it cannot prevent: Google Sheets cannot write "only if still empty", and the lock cannot stop a person or another tool. An outside write that lands between the Actor's check of the rows and its write to them can still be overwritten, and the read-back cannot see that (the Actor's own rows are there, the other writer's row is gone). How often: in the independent check's timed simulation (a tool appending about 30 rows a minute to the tab while the Actor wrote 10 runs of 2,000 rows) 3.8 % of the outside tool's rows (21 of 554) were overwritten without any message; the Actor's message never names a foreign row it overwrote. That figure was measured before the lock check was moved to right before each write, which adds about 3 seconds (two paced requests) between the check of the rows and the write; it has not been measured again, so expect it to be at least as high. The read-back also cannot see a change made after it. This is why the contract is one writer per tab. A tool that appends rows to the same tab (a form, Zapier, a script) is the case to avoid.

How to create a service account and share the sheet

You do this once.

  1. In the Google Cloud Console, pick or create a project.
  2. Open APIs & Services, find the Google Sheets API and click Enable.
  3. Open IAM & Admin, then Service Accounts, and create a service account. It needs no roles.
  4. Open the new account, go to Keys, choose Add key, then Create new key, type JSON. A key file is downloaded.
  5. Open the key file in a text editor. The line "client_email" holds the account's e-mail address, for example exporter@your-project.iam.gserviceaccount.com.
  6. Open your Google Sheet, click Share, paste that e-mail address and give it the Editor role. Untick the notification if you like; the account has no mailbox.
  7. Copy the spreadsheet ID from the sheet's link: it is the text between /d/ and /edit.
  8. In this Actor's input, paste the whole content of the key file into "Service account key (JSON)", the ID into "Spreadsheet ID", and pick your dataset.

Apify stores the key as an encrypted secret input. Delete the key in Google Cloud whenever you want to revoke access.

What it does

  1. Signs in to Google with the key. Google's own client library signs the request, and the access it asks for is limited to Google Sheets.
  2. Reads the dataset in pages, so large datasets do not have to fit in memory.
  3. Creates the tab if it is missing (just wide enough for the header, so no cells are wasted). Takes the tab lock. Writes the header row, then the rows, in batches that stay under Google's request size and rate limits. Each batch is written as one contiguous block starting at its first row, after the target rows were read and before they are read back.
  4. On "too many requests" (429) and on server errors (5xx) it waits with growing waits (up to about six and a half minutes in total for 5xx errors (318 seconds of waiting, plus up to 20 % random spread); a 429 is retried exactly once, see "Google's quota"), checks the lock and reads the target rows again, and only then sends the write again. No single write attempt can run longer than 240 seconds from its lock check to the moment the request is handed to the network. After that it stops with a clear message.
  5. Releases the lock (not after a stop that Google caused, see "The tab lock"), and writes a summary to the key-value store record OUTPUT, also when the run fails or stops unexpectedly (the key and tokens are hidden, like the log; Google's own error text is never shown, only its status and reason codes; phone numbers, tel: forms and e-mail addresses in sheetName, columnsAdded, messages and the run log are masked, and the service account address in clientEmail is shown as [hidden]@<its domain>; a token made of a phone word (the same words as everywhere else, for example tel, phone, cell, fax, kontakt, contact) followed by a number, such as tel-4915112345678, in spreadsheetId or in the project domain is masked too; bare digits and plain IDs stay). Disguised forms are not masked: a + written as %2B, a full-width +, a zero-width character inside a number, or an @ written as %40 are not recognised as contacts; do not put contact data in sheet names or column names.

Input

FieldWhat it does
datasetIdThe dataset to export. Pick one in the Console or pass its ID. In a webhook, use {{resource.defaultDatasetId}} to export the dataset of the run that triggered it.
spreadsheetIdThe part of the sheet's link between /d/ and /edit.
sheetNameThe tab to write to. Created if missing. Default Sheet1.
serviceAccountJsonThe whole JSON key file of your service account. Secret.
modeappend (default), replace or upsert.
keyFieldsUpsert only: fields that identify a row, for example id. Dots reach nested fields.
fieldsOptional list of fields to write, in column order. Empty means every field.
flattenNestedDefault on: nested objects become columns named with dots, such as address.city.
dateRenderingtext (default) keeps ISO dates as written. serial writes ISO dates and date-times as Google Sheets date numbers (UTC) that you can format as dates.
batchSizeRows read and written per request, 1 to 10,000. Default 2,000. Requests are also kept under about 1.5 MB, counted in bytes after JSON escaping. A single row that alone would be larger is skipped and counted as tooLarge.
ifTabBusyWhat this run does when another run of this Actor holds the lock of the tab: fail (default) stops at once, writes nothing and charges nothing; wait waits for the other run, also when that run creates the tab at the very moment this one starts.
waitMinutesOnly with ifTabBusy: wait: how many minutes to wait, 1 to 60. Default 10. The run then fails without writing anything.
{
"datasetId": "WkzbQMuFYuamGv3YF",
"spreadsheetId": "1AbCdEfGhIjKlMnOpQrStUvWxYz0123456789",
"sheetName": "Prices",
"serviceAccountJson": "{ ...the whole key file... }",
"mode": "upsert",
"keyFields": ["sku"],
"dateRendering": "serial"
}

With nothing filled in, the run makes no Google request, charges nothing, writes one free information row and finishes successfully. Giving only some of datasetId, spreadsheetId and serviceAccountJson is an input error that names the missing field.

Modes

  • Append adds rows below the last row that has a value in any column of the tab. Blank rows inside the data, rows you typed, notes in other columns and rows an earlier run wrote all count as used, so they are never overwritten. A tab with only a header gets its first row at row 2, an empty tab gets the header at row 1. A cell that holds a formula counts as used even when the formula shows nothing (for example =IF(A2="","",A2)). Cells that only have formatting, and no value, are not seen as used. The first free row is decided at the start of the run and saved before anything is written, so a restart writes to the same rows. Before each write the target rows are read again; if something has appeared there, the batch moves below the used range. New rows are written only in the dataset's columns; your own columns next to them are left alone (nothing, not even an empty text, is written there).
  • Replace clears the tab when the first batch is ready to be written, then writes the header and the rows. If the dataset is empty or has nothing to write, the tab is left as it is.
  • Upsert reads the key columns of the tab again before every batch (not once at the start), so the rows it updates are the rows the keys are in at that moment. If somebody sorted the tab or deleted rows during the run, the rows found now are the ones updated; a key that has vanished is appended as a new row. It never writes to a row whose current key does not match. This read is also repeated after a 429 or 5xx wait. An update is read back like a new row; an updated row that does not read back stops the run as incomplete. Keys that move between that read and the write are the window described above. For each dataset item it finds the first row with the same key and updates that row, or appends a new row below the whole used range (as in append).
    • A matched row changes only the dataset's columns: the fields of the dataset (or the ones in fields), except the key fields. Your own columns, which the dataset does not contain, are never changed or cleared, and the key cells are left as they are. If an item lacks a field of the dataset that has been seen so far in this run, that cell in the matched row is cleared, because the dataset's columns follow the item. A field that first appears in a later batch of the run does not clear the cell in rows matched earlier in the run.
    • Keys are compared as text. A key cell matches an item if either the value stored in the cell or the text the sheet shows for it equals the item's key. So 5 and "5" match, true matches TRUE, a cell formatted as a date matches the item text 2026-01-15 as well as the serial number written by dateRendering: serial, and a number formatted as 007 matches both 7 and "007". Other display formats (for example 1/15/2026) do not match; the item is then appended as a new row.
    • An item with an empty key is skipped and counted. If the same key appears twice in the dataset, the later item wins. When two rows match the same key, the first row is the one updated.

Columns and values

  • The header is row 1. Existing header cells are never renamed or moved. New fields go to the right of the last column that holds anything in the whole tab, not just of the last header cell. A column without a header that holds data is yours: the Actor never writes into it, in append or in upsert; dataset columns go only under headers the Actor owns or creates. Empty or very long (over 255 characters) field names, and fields beyond 500 columns, are not written; they are listed in the summary.
  • With fields set, only those columns are written, in that order. A listed field missing from an existing header is added to the right of everything in the tab.
  • Values are written exactly as given, never evaluated. Text such as =SUM(A1) stays text. Booleans stay booleans, numbers stay numbers, and whole numbers above 9,007,199,254,740,992 are written as text so no digits are lost.
  • Lists, and objects when flattening is off, are written as compact JSON text in one cell. A list cannot be indexed: tags.0 is not a field, so use tags and get the whole list as text.
  • null and missing fields leave the cell empty.
  • A cell holds at most 50,000 characters in Google Sheets. Longer text is cut at that limit and counted as truncatedCells in the summary.

Limits and good to know

  • A Google spreadsheet holds up to 10,000,000 cells in total, counting every tab. The Actor works out how many rows still fit before it writes. When the limit would be passed, it writes the rows that fit, then stops with a message that says so; the summary shows stoppedAtCellLimit: true.
  • Google's quota and this Actor's use of it. Google allows 60 read and 60 write requests per minute per user and project. The Actor spaces its reads at least 1.5 seconds apart, so it uses at most 40 reads a minute, and its writes at least 1.1 seconds apart (at most 54 a minute). That leaves about a third of the read quota for anything else on the same service account. Two runs at the same time on the same service account share the 60 reads: one of them may see 429 answers, which the Actor waits out (and checks the target rows again before it writes). A batch in append costs about 5 reads (check, lock check, read-back, lock confirmation, and the tab size when the grid must grow) and 3 writes (the rows, the heartbeat, and the grid growth); in upsert about 6 reads (the key columns twice, the check of new rows, the lock check, the read-back, the lock confirmation, and the tab size when the grid must grow). So 100,000 rows at the default batch size take about 6 minutes of pacing in append and about 8 minutes in upsert, plus Google's own response time (these two figures are worked out from the pacing, not measured again). The tab lock adds one write at the start and one at the end, and the heartbeat write and two reads per write attempt counted above. Refusals: a 429 is retried once per run (every request counted, the sign-in too); a Retry-After up to 30 seconds is waited, a longer one or one that cannot be read ends the run as partial. The 429 is saved before the retry is sent, so a restarted run does not get a second one (a run resurrected after a finished stop gets its one retry again). A web page where JSON belongs (a challenge, cf-mitigated, an HTML error page, also on a 2xx answer) is never retried, and neither is a 401, 403, 407, 451 or a redirect without a readable answer from the sign-in service. Waits for 5xx errors total about six and a half minutes at most. A 401 from Google ends the run at once; it is not retried.
  • Rows are written to fixed row numbers. If a run is moved to another server or restarted, it continues from the last confirmed batch, and a batch that was repeated lands on the same rows, so no row is added twice. A new run in append mode with the same data adds the rows again; use upsert for repeated loads.
  • Upsert reads the key columns of the tab again before every batch and holds them in memory while it matches the batch, so it supports tabs of up to 500,000 rows; above that the run stops before writing and suggests append. About 300 bytes per row are needed (a key is held in its stored and in its displayed form), so the default 512 MB is plenty.
  • A tab that Google reports as already existing right after the Actor asked to create it (the answer to the first request was lost) is used if it is empty. If it holds data, the run stops and writes nothing.
  • The tab grows by adding the missing rows and columns, counted from a read of the tab made just before the request. A tab is never made smaller, even if someone else resized it meanwhile, and if the answer to a grow request is lost the tab is read again instead of adding twice.
  • Merged cells: the Actor does not know about merges. Only the top-left cell of a merge holds a value, so rows below the last value that sit inside a merge look free. New rows written there go into hidden cells of the merge; no value is lost, but the rows may not be visible. Unmerge before exporting if that matters.
  • Filters and frozen rows: a filter does not stop the Actor: it reads and writes hidden rows like any other. New rows land below the filter's range, so the filter does not include them until you extend it. Frozen rows have no effect.
  • If items were read but none had anything to write (every key empty, or none of the listed fields present), the run fails with a message naming keyFields or fields, instead of finishing as if all were well. Nothing is charged for it.
  • The tab lock. The lock is a developer-metadata record on the spreadsheet, one per tab (its key holds the tab's sheet ID; its value holds the run ID and a heartbeat time). The design relies on Google refusing to create a second record with an ID that already exists, so that of two runs that start at the same moment only one gets it. This is unverified: Google's reference does not state it, and it will not be treated as fact until the real-API acceptance run (check 14b) has passed. Until then, do not rely on it. The run refreshes the heartbeat at least once a minute and right before every write, and deletes the record when it finishes, whatever the outcome. A run that crashes, is killed or is stalled leaves the record behind; a record whose heartbeat is 10 minutes old counts as dead and the next run takes it over (with one request that can succeed only for one run). A resurrected run keeps its run ID and takes its own record back at once. A run that finds its record gone or taken (it was stalled for more than 10 minutes), or cannot confirm it, does not send the write that was about to go out: it stops with incomplete and names the rows. The one case it cannot stop is a request that is already on the wire when the lock is taken, which the transport closes at its total deadline of 90 seconds (the whole request, not a wait per network step, except the name lookup and the connect, which the timer does not cut), far below the 10 minutes. After a stop that Google caused (a refusal, a web page where data belongs, a quota stop, a key error, a sharing or editor error, the Sheets API switched off, any other Google 4xx answer, or Google not answering after 9 tries) the run sends no request at all to Google, so it does not release the lock either: resurrect the run (it takes its own record back at once), or wait up to 10 minutes, after which the record has ended by itself and a new run can take over. A new run in between is refused as busy, and nothing is written or charged. After a stop Google did not cause (the maximum charge, the end of the data) the lock is released. The record is visible to anybody who opens the spreadsheet with the Sheets API; it does not show in the Sheets screen. The heartbeat is compared against the clock of the machine that reads it, so a clock that is off by minutes can shorten or lengthen the 10 minutes.
  • What the lock does not do. It stops this Actor's own runs. A person, another tool, or another script that does not look at the record can still write to the tab. For them the Actor checks and reads back, as described at the top, and the contract is: one writer per tab.
  • If two webhook deliveries start two runs for the same dataset, the second stops at the lock (default) or waits and then exports the same dataset again: in append mode the rows are then in the tab twice and charged twice. Use upsert for repeated loads.
  • After a run stops because rows did not read back or kept being taken, the rows of that batch that are already in the tab may appear twice if the run is resumed; they were not charged.
  • On Google's side, only the Sheets API and Google's sign-in service (sheets.googleapis.com and oauth2.googleapis.com) are contacted; the key file's own token address is ignored.
  • The dataset must be one the run can read with its own access. The run only reads it.

The key stays secret

The key is a secret input field. The Actor uses it to sign one request to Google's sign-in service and keeps only the short-lived access token in memory. The key and the token are not written to the log, the key-value store, the dataset or error messages. The client_email is shown only as [hidden]@<its domain> (or [removed] when it looks like a personal address) in the summary and in errors; you have the full address in your key file, and the domain tells which project it belongs to.

Output

The OUTPUT record in the key-value store:

{
"status": "complete",
"message": "Wrote 4980 new and updated 20 rows in 'Prices'.",
"mode": "upsert",
"spreadsheetId": "1AbCdEfGhIjKlMnOpQrStUvWxYz0123456789",
"sheetName": "Prices",
"clientEmail": "[hidden]@your-project.iam.gserviceaccount.com",
"rowsRead": 5003,
"rowsWritten": 4980,
"rowsUpdated": 20,
"rowsSkipped": 3,
"skipped": { "notObject": 0, "empty": 1, "noKey": 2, "tooLarge": 0 },
"truncatedCells": 0,
"columns": 9,
"columnsAdded": ["discount"],
"columnsDropped": [],
"stoppedAtMaxCharge": false,
"stoppedAtCellLimit": false,
"apiRetries": 2,
"resumed": false
}

status is:

  • complete: every row was written and read back as sent.
  • partial: stopped early and resumable: at your maximum charge, or because Google's Sheets API kept answering 429 (this Actor retries a 429 once per run, waits for a Retry-After of at most 30 seconds, and treats a longer or unreadable one as a stop), or answered with a web page (a challenge or an error page) where data belongs, which is never retried. The stop is saved and rows that were not confirmed are not charged.
  • incomplete: the run stopped because it could no longer vouch for its rows: rows did not read back as written, other writers kept taking the rows it was about to write, a header cell it was adding was changed, or it lost its lock. The message names the rows. Rows confirmed in earlier batches are kept and charged; the rows named were not charged. The Apify run is marked failed. Check the tab and resurrect the run to continue.
  • failed: anything else, including a tab that was busy (another run holds its lock; nothing was written or charged), bad input, Google errors, and a crash. A run that crashes unexpectedly still writes status: failed with the rows written so far.

The run's own dataset stays empty, with one exception: a run started without input (no datasetId, spreadsheetId and serviceAccountJson, for example the Store's default test run) makes no Google request, charges nothing and writes one free information row (type: info, charged: false) saying what to set. It is not written twice in a restarted run.

Pricing

Pay per event:

  • Row written or updated: USD 0.0002 per row that was written and then read back from your sheet exactly as sent (USD 0.20 per 1,000 rows).
  • Export run: USD 0.002 once per run, charged after the first rows were read back.
  • Actor start: USD 0.00005 per run, charged by Apify when the run starts.

What counts as a row: one charge per distinct sheet row written or updated in the run. In upsert, when the same key appears several times in the dataset, the row is charged once, however batchSize splits the dataset. A row that was only skipped is not charged. A new run of the same data charges again.

A row is charged after the read-back confirms it. The export-run fee is charged once, after the first rows were read back, and is not refunded: a run that fails or stops later has already paid it, and so has a run that ends incomplete after confirming rows. A batch that fails or does not read back is not charged, skipped items are not charged, a run that is refused because the tab is busy writes nothing and pays only Apify's start fee, and so does any run that writes nothing. The read-back is made right after the write: a change another writer makes later is not seen, and a row already confirmed and charged stays charged. If you set a maximum charge per run, the run stops before a row would pass it; no row is written that you did not pay for, and stoppedAtMaxCharge is true.

Errors

You seeWhat to do
The key is not valid JSON, or is not a service account keyPaste the whole content of the downloaded JSON key file. OAuth client files and API keys do not work.
The spreadsheet is not shared with the service accountOpen the sheet, click Share, add the service account as Editor. Its address is client_email in your key file; the message and the summary show only its domain.
The account can open the sheet but may not change itChange its role to Editor.
Spreadsheet not foundCheck spreadsheetId: the text between /d/ and /edit.
The Google Sheets API is switched offEnable the Google Sheets API in the Google Cloud project that owns the service account.
Google rejected the sign-inThe key was deleted or disabled in Google Cloud, or the file is incomplete. Create a new key.
Stopped: Google kept answering "too many requests", or asked for a longer wait, or answered with a web pageThe run ends partial (the stop is saved) and charges nothing for rows that were not confirmed. Wait a few minutes and resurrect the run; it continues without writing rows twice.
Another run of this Actor is writing to the tabOne writer per tab: wait until it has finished, use another tab, or set ifTabBusy to wait. If that run crashed, its lock goes stale after 10 minutes. Nothing was written or charged.
Status incomplete: rows did not read back, or other writers kept taking the rowsSomething else wrote to the tab during the run. Stop it, look at the rows named in the message, then resurrect the run.
This run lost its lockThe run was stalled for more than 10 minutes and another run took the tab. Check the tab, then resurrect.
No item had a value in every key field, or none of the listed fieldsCheck keyFields / fields: use column names after flattening, with dots for nested fields.
10,000,000 cells reachedDelete unused tabs or rows, or use another spreadsheet.

FAQ

Can I write to a tab that already has data? Yes. Append adds below everything in the tab, upsert updates only the dataset's columns of matching rows and adds the rest below, replace clears it first.

Does it change my sheet's formatting? No. It writes values only. A new tab starts with 1,000 rows, as many columns as the header needs, and Google's default look.

Can I use one service account for many sheets? Yes. Share each sheet with the same e-mail.

Can a form, Zapier or a person add rows to the tab while the export runs? Not by the contract. The Actor notices most such writes and moves out of their way, but one that lands between its check and its write can be overwritten (3.8 % of an outside appender's rows in the timed simulation described at the top, with no message that names them). Use a tab that belongs to this export, and copy from it.

Can I start the export from a webhook of every scraper run? Yes, if the runs do not overlap on the same tab. For overlapping runs use ifTabBusy: wait, or one tab per scraper.