> ## Documentation Index
> Fetch the complete documentation index at: https://docs.adriel.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Refresh a Google Sheets Export

> Keep a Google Sheet in sync with a dynamic data link: refresh the link on demand in Adriel and automate the sheet update with Apps Script.

A dynamic data link keeps a Google Sheet connected to a table widget, but two separate refreshes decide how fresh the sheet is. Adriel regenerates the data behind the link, and Google Sheets re-reads the link. This page covers both, and how to automate the Google Sheets side with an Apps Script so the sheet updates without manual edits.

For creating the link itself, see [Exporting widget data](https://docs.adriel.com/widgets/widget-actions/exporting-widget-data.md#export-to-gss-google-sheets).

## How a dynamic data link updates

Adriel regenerates the data behind a dynamic data link **every 4 hours**. To see when it last ran, open the table widget's **3 vertical dots** menu, click **Export to GSS**, and check **Last updated** under **Dynamic data link**.

To regenerate the data immediately instead of waiting for the next cycle, click **Refresh now** next to **Last updated**. Processing time depends on the size of the table. When it finishes, **Last updated** shows the new time.

The link always uses the latest published version of the dashboard, including its saved date range. A date range changed only while viewing the dashboard is not applied to the link.

## Why the sheet can still show older data

Google Sheets caches the result of `IMPORTDATA` and recalculates it about once an hour ([Google Docs Editors Help](https://support.google.com/docs/answer/58515)). So even after Adriel regenerates the link, a sheet that uses `=IMPORTDATA("link")` can keep showing the previous data for up to an hour.

To pull the latest data into the sheet right away, change the URL inside the formula so Google Sheets treats it as a new request. For example, add `&v=2` to the end of the link, and increase the number each time:

```text theme={null}
=IMPORTDATA("https://api.adriel.com/api/widgetExport/auto?exportKey=YOUR_EXPORT_KEY&v=2")
```

The extra parameter doesn't change the data the link returns. To skip this manual step, use the Apps Script below.

## Automate the refresh with Apps Script

The script fetches the dynamic data link directly and writes the result into a tab as plain values. Compared with `IMPORTDATA`:

* It is not cached by Google Sheets, so each run returns the latest data Adriel has generated.
* If a fetch fails, the previous data stays in the tab instead of turning into an error.
* The values can be edited, sorted, and referenced like any other cells. They are replaced on the next run.

> [!WARNING]
>
> **The script clears the whole target tab on every run.** Use a dedicated tab for the Adriel data, and keep formulas, notes, and calculations on other tabs that reference it. Do not put an `IMPORTDATA` formula on the same tab.

**1. Copy the dynamic data link**

In Adriel, open the table widget's **3 vertical dots** menu, click **Export to GSS**, and click **Copy dynamic data link**.

**2. Open the Apps Script editor**

In the Google Sheet, create or pick the tab that will hold the data and note its exact name. Then click **Extensions** > **Apps Script**.

**3. Paste the script**

Delete any code in the editor and paste the script below.

**4. Enter the link and the tab name**

Replace `PASTE_YOUR_DYNAMIC_DATA_LINK_HERE` with the copied link and `PASTE_YOUR_TAB_NAME_HERE` with the tab name. Keep the quotation marks. The tab name must match exactly, including spaces and case.

**5. Save and run once**

Click **Save**, select `refreshAdrielData` in the function list, and click **Run**. On the first run, Google asks for permission to access the spreadsheet and external services. Review and allow it.

**6. Check the sheet**

Return to the sheet and confirm the data appears in the target tab. Reload the sheet to add the **Adriel** > **Refresh data** menu for refreshing on demand.

```javascript theme={null}
/**
 * Fetches the latest data from an Adriel dynamic data link and writes it
 * into a tab as values. Unlike IMPORTDATA, the result is not cached by
 * Google Sheets, and a failed fetch keeps the previous data in place.
 */

// 1. Paste the dynamic data link copied from Adriel.
const CSV_URL = "PASTE_YOUR_DYNAMIC_DATA_LINK_HERE";

// 2. Enter the exact name of the tab to write to.
//    Everything on this tab is replaced on each refresh.
const SHEET_NAME = "PASTE_YOUR_TAB_NAME_HERE";

function refreshAdrielData() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = spreadsheet.getSheetByName(SHEET_NAME);
  if (!sheet) {
    notify_(`Tab "${SHEET_NAME}" was not found. Check SHEET_NAME.`);
    return;
  }

  // A unique timestamp on every request makes sure no cached copy is returned.
  const url = CSV_URL + (CSV_URL.includes("?") ? "&" : "?") + "_ts=" + Date.now();
  const response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });

  if (response.getResponseCode() !== 200) {
    notify_(`Could not load data (HTTP ${response.getResponseCode()}). The previous data was kept.`);
    return;
  }

  const rows = Utilities.parseCsv(response.getContentText("UTF-8"));
  if (rows.length === 0) {
    notify_("The link returned no data. The previous data was kept.");
    return;
  }

  sheet.clearContents();
  sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
  spreadsheet.toast("Data refreshed.", "Adriel", 3);
}

// Adds an Adriel > Refresh data menu when the sheet opens.
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu("Adriel")
    .addItem("Refresh data", "refreshAdrielData")
    .addToUi();
}

// Shows an alert when run from the menu or editor. Alerts are unavailable
// in time-driven triggers, so the error is thrown instead, which makes
// Google send its trigger failure email.
function notify_(message) {
  try {
    SpreadsheetApp.getUi().alert(message);
  } catch (e) {
    throw new Error(message);
  }
}
```

### Run the script on a schedule

To refresh the tab automatically, add a time-driven trigger in the Apps Script editor ([Apps Script triggers guide](https://developers.google.com/apps-script/guides/triggers/installable)).

**1. Open the triggers page**

In the Apps Script editor, click **Triggers** (clock icon) on the left.

**2. Add a trigger**

Click **Add Trigger**. Set the function to `refreshAdrielData`, the event source to **Time-driven**, and pick an interval.

**3. Save the trigger**

Click **Save**.

Adriel regenerates the link every 4 hours, so an hourly trigger keeps the sheet within an hour of the latest version. After clicking **Refresh now** in Adriel, use **Adriel** > **Refresh data** in the sheet once **Last updated** shows the new time.

> [!NOTE]
>
> A trigger runs under the Google account that created it. If a scheduled run fails, Google emails that account a failure summary.

## Troubleshooting

> [accordion] The script says the tab was not found
>
> `SHEET_NAME` doesn't match any tab. Copy the tab name exactly, including spaces and case, and save the script again.

> [accordion] The script says it could not load data
>
> The link returned an error, so the previous data was kept. Check that `CSV_URL` contains the full dynamic data link copied from Adriel. If the dashboard is password protected, the data isn't accessible from Google Sheets.

> [accordion] The sheet shows older data than Adriel
>
> Compare the time of the last script run with **Last updated** in the **Export to GSS** modal. If Adriel hasn't regenerated the link yet, click **Refresh now**, wait for **Last updated** to change, and run the script again.

> [accordion] Google shows an authorization or OAuth error
>
> Authorization for Apps Script is managed in the Google account or Workspace, not in Adriel. See [Why does a Google Apps Script OAuth error occur](https://docs.adriel.com/data-sources/g-n/google-sheets/faqs.md) for what to check.

## Related

> [card] [Exporting widget data](https://docs.adriel.com/widgets/widget-actions/exporting-widget-data.md)
>
> Export a widget to PDF, CSV, XLSX, Google Sheets, or email.

> [card] [Scheduled exports](https://docs.adriel.com/automate-and-ai/scheduled-exports.md)
>
> Manage recurring exports across the workspace.

> [card] [Google Sheets FAQs](https://docs.adriel.com/data-sources/g-n/google-sheets/faqs.md)
>
> Common questions about Google Sheets and Adriel, including Apps Script OAuth errors.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.