# Google Sheets Import Export (`apify/google-sheets-import-export`) Actor

Easy way to import and export data from datasets across multiple Google sheets.

Apify does not use Google Workspace APIs  to develop, improve, or train generalized AI and/or ML models.

- **URL**: https://apify.com/apify/google-sheets-import-export.md
- **Developed by:** [Apify](https://apify.com/apify) (community)
- **Categories:** Automation, Integrations, Open source
- **Stats:** 2 total users, 0 monthly users, 0.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

Pay per usage

This Actor is paid per platform usage. The Actor is free to use, and you only pay for the Apify platform usage, which gets cheaper the higher subscription plan you have.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-usage

## What's an Apify Actor?

Actors are web data automations that power AI and operations. They run on the Apify platform to scrape websites, process data, connect APIs, and automate workflows.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.
Actors are written with capital "A".

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.
The best way to integrate Actors is as follows.

- **AI agents and MCP clients** — the [Apify MCP server](https://docs.apify.com/integrations/mcp.md) at `https://mcp.apify.com` (remote, streamable HTTP, OAuth on first use).
- **Agentic workflows and local Actor development** — [Agent Skills](https://apify.com/.well-known/agent-skills/index.json) with the [Apify CLI](https://docs.apify.com/cli/docs.md): `npm install -g apify-cli`, then `apify login`.
- **JavaScript/TypeScript projects** — the official [JS/TS client](https://docs.apify.com/api/client/js/docs.md): `npm install apify-client`.
- **Python projects** — the official [Python client](https://docs.apify.com/api/client/python/docs.md): `pip install apify-client`.
- **Any other language** — the [REST API](https://docs.apify.com/api/v2.md).

For usage examples, see the [API](#api) section below.

For more details, see Apify documentation as [Markdown index](https://docs.apify.com/llms.txt) and [Markdown full-text](https://docs.apify.com/llms-full.txt).

# README

## Google Sheets Import & Export Data

- [Why use Google Sheets Import & Export](#why-use-google-sheets-import-and-export)
- [How to use Google Sheets Import & Export](#how-to-use-google-sheets-import-and-export)
- [Input settings](#input-settings)
- [Limits](#limits)
- [Authentication and authorization](#authentication-and-authorization)
- [Public spreadsheet (no authorization)](#public-spreadsheet-no-authorization)
- [Local usage](#local-usage)
- [Important tips](#important-tips)
- [Modes](#modes)
- [Raw data import](#raw-data-import)
- [Raw data table format (array of arrays)](#raw-data-table-format-array-of-arrays)
- [Dataset format (array of objects)](#dataset-format-array-of-objects)
- [Further reading](#further-reading)
- [Changelog](#changelog)

### Why use Google Sheets Import and Export?

If you're looking for an easy way to import and export data from datasets across multiple Google sheets, Google Sheets Import & Export is the Apify automation tool for you.

It can process data in your current spreadsheet, import new data from [Apify datasets](https://www.apify.com/docs/storage#dataset), or a raw JSON file. It can be run both on the Apify platform or locally.

You can use this actor with any programming language of your choice by calling [Apify API](https://www.apify.com/docs/api/v2).

### How to use Google Sheets Import and Export

We have an in-depth [tutorial](https://blog.apify.com/google-sheets-import-data/) on how to use this tool.

### Input settings

You can specify the following settings:

- Mode
- Spreadsheet id
- Public spreadsheet (read-only)
- Dataset ID
- Raw data
- Limit items
- Offset items
- Deduplicate by field
- Deduplicate by equality
- Transform function
- Range
- Columns order
- Keep column order from sheet
- Google OAuth tokens store
- Create backup
- Backup store id
- Google Developer Console credentials

For a complete description of all settings, see [input specification](https://apify.com/lukaskrivka/google-sheets/input-schema).

### Limits

If you exceed these limits, the actor run will fail, and no data will be imported.

- **Maximum runs (imports) per 100 seconds: 100**

### Authentication and authorization

If you are using this actor for the first time, you have to log in with the Google account where the spreadsheet is located. You then need to authorize Apify to work with your spreadsheets. Internally at Apify, we use our small npm package [apify-google-auth](https://www.npmjs.com/package/apify-google-auth). Please check this [article](https://help.apify.com/en/articles/2424053-google-integration) on how to handle the authorization process.

After authorization, tokens are stored in your key-value store, and you don't need to authorize again. So, after the first usage, you can fully automate the actor.

**Please note that Google gradually makes it harder to authenticate and it is possible it will not work for you at all. Try a different gmail, a company one rather then personal, or use directly our [integration](https://apify.com/integrations) for Google drive.**

### Public spreadsheet (no authorization)

If you don't mind publishing your spreadsheet, you can use this actor without authorization for **read mode**.
Simply set the input **publicSpreadsheet** to **"true"**.

To protect against the abuse of Apify's unofficial Google API, a public spreadsheet without authorization will only work on the Apify platform by using a secret environment variable.

If you want to run the public mode locally, you have to create your project in the Google console and pass an API\_KEY environment variable to your actor process.

Example:
**API\_KEY=AIzaSyAPijSDFsdfSSf3kvGVsdfsdfsdsdnAVbcZb5Y apify run -p**

### Local usage

The official actor relies on the CLIENT\_SECRET environment variable being set. This assures that official API integration is used.

If you want to use this actor locally or fork the source code, you will need to create your own project in [Google Developer Console](https://console.developers.google.com/), create your own credentials, and pass them correctly to the **googleCredentials** input variable. This is explained further in the [Apify Google Auth library](https://www.npmjs.com/package/apify-google-auth).

### Important tips

- The maximum number of cells in the whole spreadsheet is 2 million! If the actor ever tries to import data over this limit, it will just throw an error, finish and not import anything. In this case, use more spreadsheets.

- No matter which mode you choose, the actor recalculates how the data should be positioned in the sheet, updates all the cells, and then trims the exceeding rows and columns. This ensures that the sheet always has the optimal number of rows and columns and there is enough space for the newly generated data.

- The actor parsing follows the default [Google Sheets parsing](https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption). Therefore, depending on the configuration of your system, constructions such as `"1.1"` and `"1,1"` can be interpreted either as a number or a string (text). For this reason, it is recommended that you always use valid JSON numbers (e.g. `1.1`).

### Modes

This actor can be run in multiple different modes. Each run must have only one specific mode. *Mode* also affects how other options work (details are explained in the specific options).

- **replace:** If there's any old data in the sheet, it is cleaned, and then new data is imported.

- **append:** This mode adds new data as additional rows below the old rows already present in the sheet. Keep in mind that the columns are recalculated so some of them may move to different cells if new columns are added in the middle.

- **modify:** This mode doesn't import anything. It only loads the data from your sheets and applies any of the processing you set in the options.

- **read:** This mode simply loads the data from the spreadsheet, optionally can process them, and saves them as 'OUTPUT' JSON file to the default key-value store.

- **load backup:** This mode simply loads any backup rows from previous runs (look at the backup option for details) and imports it to a sheet in the replace mode.

### Raw data import

If you want to send data in raw JSON format, you need to pass the data to the `rawData` input parameter. You will also need to have an Apify account so we can properly store your Google authentication tokens (you can opt out at anytime).

> **Important!** - Raw data cannot exceed 9MB, as this the default limit for Apify actor inputs. If you want to upload more data, you can easily split it into more runs (they're fast and cheap).

##### Raw data table format (array of arrays)

`rawData` should be an array of arrays where each of the arrays represents one row in the sheet. The first row should be a header row where the field names are defined. Every other row is a data row.

It is important to have a proper order in each array. If the field is null for any row, the array should contain an empty string in that index. Data rows can have a smaller length than the header row but if they are longer the extra data will be trimmed off.

Arrays **cannot** contain nested structures like objects or other arrays! You have to flatten them in a format where `/` is a delimiter. E.g. `personal/hobbies/0`.

```
"rawData": [
  ["name", "occupation", "email", "hobbies/0", "hobbies/1"],
  ["John Doe", "developer", "john@google.com", "sport", "movies with Leonardo"],
  ["Leonardo DiCaprio", "actor", "leonardo@google.com", "being rich", "climate change activism"]
]

```

##### Dataset format (array of objects)

`rawData` should be an array of objects where each object represents one row in the sheet. The keys of the objects will be transformed to a header row and the values will be inserted into the data rows. Objects don't need to have the same keys. If an object doesn't have a key that another object has, the row will have an empty cell in that field.

The object **can** contain nested structures (objects and arrays) but in that case, it will call Apify API to flatten the data which can take a little more time on large uploads so try to prefer flattened data.

*Nested*:

```
"rawData": [
  {
    "name": "John Doe",
    "email": "john@google.com",
    "hobbies": ["sport", "movies with Leonardo", "dog walking"]
  },
  {
    "name": "Leonardo DiCaprio",
    "email": "leonardo@google.com",
    "hobbies": ["being rich", "climate change activism"]
  }
]

```

*Flattened*:

```
"rawData": [
  {
    "name": "John Doe",
    "email": "john@google.com",
    "hobbies/0": "sport",
    "hobbies/1": "movies with Leonardo",
    "hobbies/2": "dog walking"
  },
  {
    "name": "Leonardo DiCaprio",
    "email": "leonardo@google.com",
    "hobbies/0": "being rich",
    "hobbies/1": "climate change activism"
  }
]

```

### Further reading

If you want a much deeper understanding of how this actor works, consult [Google Sheets for developers](https://developers.google.com/sheets/api/).

### Changelog

A detailed list of changes is in the [CHANGELOG.md](https://github.com/metalwarrior665/actor-google-sheets/blob/master/CHANGELOG.md) file.

# Actor input Schema

## `oAuthAccount.wrhyqVCM1DRfhvEJm` (type: `string`):

Google Account Id

## `sheetId.wrhyqVCM1DRfhvEJm` (type: `string`):

Spreadsheet from where the old data will be loaded

## `mode` (type: `string`):

What should the actor do

## `publicSpreadsheet` (type: `boolean`):

If checked, you don't need to authorize. You have to publish your spreadsheet and it works only in read mode

## `datasetId` (type: `string`):

Dataset or crawler execution id where the new data will be loaded from

## `rawData` (type: `array`):

Raw data JSON array. Can be array of arrays for direct row import or arrays of objects.

## `limit` (type: `integer`):

Number of items to take from the dataset. The default is 250000.

## `offset` (type: `integer`):

Number of items to skip from the dataset. Default is 0.

## `deduplicateByField` (type: `string`):

Items will be deduplicated by a value of this field. Cannot be used together with 'Deduplicate by equality' or 'Transform function'.

## `deduplicateByEquality` (type: `boolean`):

Items will be deduplicated if they are the same. Cannot be used together with 'Deduplicate by field' or 'Transform function'.

## `transformFunction` (type: `string`):

Custom function that will take new items and old items arrays as parameters and produces final array that will be imported.  Cannot be used together with 'Deduplicate by equality' or 'Deduplicate by field'

## `range` (type: `string`):

Range of the spreadsheet in A1 notation where the actor should operate. Default is the first sheet.

## `columnsOrder` (type: `array`):

Array of keys. First sorts the columns by provided keys. The rest is sorted alphabetically.

## `keepSheetColumnOrder` (type: `boolean`):

If true, keeps the order of columns as they are in the sheet. If there is no sheet data yet, this does nothing.

## `tokensStore` (type: `string`):

Key-value store where your Google OAuth tokens will be stored so you don't have to authorize every time again. By default it is google-oauth-tokens

## `createBackup` (type: `boolean`):

Old rows from your spreadsheet will be saved to the default key-value store before importing new rows.

## `backupStore` (type: `string`):

Id of the key-value store where the backup you want to load is located. Can pnly be used if mode is 'load-backup'

## `googleCredentials` (type: `object`):

If you want to use this actor locally or with your own version, you have to provide your own crednetials. Check actor readme for more information.

## Actor input object example

```json
{
  "mode": "append",
  "publicSpreadsheet": false,
  "limit": 250000,
  "offset": 0,
  "transformFunction": "// Uncomment this code only if you don't use \"Deduplicate by field\" or \"Deduplicate by equality\"\n// This code behaves as if there was no transform function\n/*({ spreadsheetData, datasetData }) => {\n    return spreadsheetData.concat(datasetData);\n}*/",
  "tokensStore": "google-oauth-tokens"
}
```

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "transformFunction": `// Uncomment this code only if you don't use "Deduplicate by field" or "Deduplicate by equality"
// This code behaves as if there was no transform function
/*({ spreadsheetData, datasetData }) => {
    return spreadsheetData.concat(datasetData);
}*/`
};

// Run the Actor and wait for it to finish
const run = await client.actor("apify/google-sheets-import-export").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "transformFunction": """// Uncomment this code only if you don't use \"Deduplicate by field\" or \"Deduplicate by equality\"
// This code behaves as if there was no transform function
/*({ spreadsheetData, datasetData }) => {
    return spreadsheetData.concat(datasetData);
}*/""" }

# Run the Actor and wait for it to finish
run = client.actor("apify/google-sheets-import-export").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "transformFunction": "// Uncomment this code only if you don'\''t use \\"Deduplicate by field\\" or \\"Deduplicate by equality\\"\\n// This code behaves as if there was no transform function\\n/*({ spreadsheetData, datasetData }) => {\\n    return spreadsheetData.concat(datasetData);\\n}*/"
}' |
apify call apify/google-sheets-import-export --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,apify/google-sheets-import-export"
        }
    }
}

```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/RsY1SbuvxRiFNvxdu/builds/a2drCfbjQeL8JbFUJ/openapi.json
