Skip to main content
The Export tab of a report, with the REST API endpoints

Reports > Sales overview > Export

Open in TWICE Admin

reports
Add a script to a Google Sheet that fetches the revenue and payments ledgers into two tabs, and run it on a timer. Use Apps Script because IMPORTDATA cannot send the API key header.

Prerequisites

Required access: a TWICE Commerce API key. It carries owner-level permissions, and anyone with edit access to the sheet can open the script and read it. Keep the sheet’s editors to people you would give the key to, and share read-only copies or dashboards with everyone else. See API keys.
Before you start:
  • The ledger endpoints and file format from Pull your data into a BI tool.
  • Your currency code and the first date you want history from.
  • A size check. A Google Sheet holds 10 million cells, and a ledger row is about 30 cells, so a sheet fits roughly 300,000 ledger rows across both tabs.

The walkthrough

1

Open the script editor

Create a Google Sheet, then choose Extensions → Apps Script.
2

Store the key as a script property

Open Project Settings (the gear icon), select Add script property, set Property to TWICE_API_KEY and Value to the key, and save.
3

Paste the script

Open Editor, replace the contents of Code.gs with the script below, and change CURRENCY and FROM_DATE to yours. Save.
4

Run it once

Select refreshTwiceLedgers in the function menu and choose Run. Approve the permissions Google asks for: the script reads the sheet and calls an external URL.
5

Schedule it

Open Triggers (the clock icon) and choose Add Trigger. Pick refreshTwiceLedgers, set the event source to Time-driven, pick Day timer and an hour, and save.
Use this script:
The script replaces each tab on every run, so the tabs always hold the whole period from FROM_DATE to today. Point formulas, pivot tables and charts at the tabs, never at a fixed range, because the row count grows.

How do I know it worked?

  • The sheet has a Revenue tab and a Payments tab, each starting with a header row.
  • A pivot table on the Revenue tab summing Amount_Total, filtered to Started_In_Period = Yes, matches the Revenue report in the admin for the same period and currency.
  • Executions in the Apps Script editor lists a completed run started by the trigger.

Troubleshooting / common pitfalls

The script property is missing or holds an old key. Check TWICE_API_KEY under Project Settings, and that the key still exists under Settings → Integrations & API in TWICE Commerce.
The period holds more than 100,000 rows. Move FROM_DATE later, or load one tab per year by calling loadLedger_ once per year with its own dates and sheet name.
One run may take six minutes. Load the revenue and payments ledgers from two functions on two triggers an hour apart.
A formula or format on the tab was set before the load. Clear the tab’s formatting and let the script write the values again.
Google documents the calls the script uses in UrlFetchApp, Properties service and Installable triggers.

Pull your data into a BI tool

The endpoints, the file format and the rate limits.

Connect Looker Studio

Build dashboards on this sheet.

Connect Tableau

Point Tableau at this sheet.

Finance reports

Every ledger column, and how each number is derived.