Skip to main content

📗 Connect Google Sheets to Papertrail

Load your Papertrail data into a Google Sheet and refresh it automatically every day.

D
Written by Daniel Nascimento

Load your Papertrail records into a Google Sheet and refresh it automatically every day. It takes about 10 minutes.

This guide builds one example: a table of every record in one account, with its folder and state. It's a starting point you can adapt. See Build Your Own Reports below.

Before you start you'll need:


Step 1: Add the Script

Copy this code. You'll paste it in during the steps below.

const ACCOUNT_ID = 'PASTE-YOUR-ACCOUNT-ID-HERE';
const SHEET_NAME = 'Papertrail records';

// Each column: the heading in the sheet, and how to read it from a record.
const COLUMNS = {
'Record ID': r => r.id,
'Name': r => r.name,
'Folder': r => r.folder?.data?.name,
'State': r => r.state?.data?.name,
'Next check': r => r.next_check_at,
'Due': r => r.is_due,
'Overdue': r => r.is_overdue,
'Archived': r => r.archived_at,
'Last updated': r => r.updated_at,
};

// Lists your accounts so you can copy the account ID. Run once.
function listAccounts() {
callPapertrail_('/accounts').data.forEach(a => console.log(a.id + ' ' + a.name));
}

// Replaces the sheet's contents with every record in the account.
function refreshPapertrailRecords() {
const rows = [];
let after = null;
do {
let path = '/accounts/' + ACCOUNT_ID + '/records?limit=500&include=state,folder';
if (after) path += '&after=' + encodeURIComponent(after);
const page = callPapertrail_(path);
page.data.forEach(r => rows.push(Object.values(COLUMNS).map(read => read(r) ?? '')));
after = page.meta.cursor.next;
} while (after);

const book = SpreadsheetApp.getActive();
const sheet = book.getSheetByName(SHEET_NAME) || book.insertSheet(SHEET_NAME);
const headings = Object.keys(COLUMNS);
sheet.clearContents();
sheet.getRange(1, 1, 1, headings.length).setValues([headings]);
if (rows.length) sheet.getRange(2, 1, rows.length, headings.length).setValues(rows);
}

// Calls Papertrail. If asked to slow down, waits as long as it says (up to 5 times).
function callPapertrail_(path) {
const key = PropertiesService.getScriptProperties().getProperty('PAPERTRAIL_API_KEY');
for (let attempt = 1; attempt <= 5; attempt++) {
const res = UrlFetchApp.fetch('https://api.papertrail.io' + path, {
headers: {
Authorization: 'Bearer ' + key,
Accept: 'application/vnd.papertrail.api+json; version=1.0',
},
muteHttpExceptions: true,
});
const status = res.getResponseCode();
if (status === 200) return JSON.parse(res.getContentText());
if (status === 401) throw new Error('Your Papertrail API key is wrong, has expired or was revoked. Create a new key and update the script property.');
if (status === 403) throw new Error("This API key can't read that account. Check the account ID, and that you're an admin on the account.");
if (status !== 429) throw new Error("Papertrail couldn't complete the refresh. Try again later.");

const headers = res.getHeaders();
const wait = Number(headers['Retry-After'] || headers['retry-after'] || 60);
if (wait > 300) throw new Error('This API key has reached its daily limit. The next refresh after midnight UTC will work.');
Utilities.sleep(wait * 1000);
}
throw new Error('Papertrail is busy. The next scheduled refresh will try again.');
}
  • Open your Google Sheet and give it a name, such as 'Papertrail asset register'.

  • Select Extensions → Apps Script. A new tab opens.

  • Click Untitled project at the top and rename it 'Papertrail records'. Google shows this name when it asks for permission, so it's worth giving it a clear one.

  • Delete everything in the editor and paste the code above.

  • Click Save (the disk icon). Unsaved changes disappears from the top of the page.


Step 2: Store Your API Key

The key goes in the script's settings, not the code, so it doesn't show in the editor.

  • In Apps Script click Project Settings (the cog icon on the left).

  • Scroll to Script properties and click Add script property.

  • Property: PAPERTRAIL_API_KEY. Value: paste your API key.

  • Click Save script properties.


Step 3: Find Your Account ID

  • Go back to the Editor (the < > icon).

  • In the function dropdown next to Debug, choose listAccounts and click Run.

  • The first time, Google asks for permission. Click Review permissions and choose your Google account.

  • Make sure Select all is ticked, then click Continue. The script needs to edit your sheet and connect to Papertrail.

  • The Execution log opens and lists your accounts. Copy the ID of the one you want.

  • Paste it in place of PASTE-YOUR-ACCOUNT-ID-HERE on the first line and click Save.


Step 4: Load Your Records

  • Choose refreshPapertrailRecords in the function dropdown and click Run.

  • Go back to your sheet. A Papertrail records tab now holds one row per record.


Step 5: Refresh Every Day

  • In Apps Script click Triggers (the clock icon on the left).

  • Click Add Trigger in the bottom right.

  • Set the function to refreshPapertrailRecords, the event source to Time-driven, the type to Day timer, and the time to a quiet hour, such as 6am to 7am. Leave the other settings as they are.

  • Click Save.


How You Know It Worked

The number of rows, not counting the heading row, matches the number of records in your Papertrail account.


Build Your Own Reports

The recipe above is just one example. Your API key can read the same Papertrail data you see when you sign in, so you can also report on:

  • Subscriptions and accounts: several accounts side by side.

  • Checklists: the questions and answer options on each checklist.

  • Schedules and frequencies: which checklists each schedule uses and how often it repeats.

  • Inspections: each record's completed inspections with their answers.

  • Members: who has access to each account, and their role.

You can change the columns, or combine Papertrail data with other data in your spreadsheet.

The API documentation lists everything you can read, what each part is for and how to ask for it. Open it in Papertrail from Menu → Integrations → API documentation, or go to app.papertrail.io/integrations/api-docs.

If you need help building something different, or something isn't available yet, contact our Support Team so we can help and prioritise it.

Whatever you build, the key is read-only: it can't create, edit or delete anything in Papertrail.


If Something Goes Wrong

You see

What to do

"Your Papertrail API key is wrong, has expired or was revoked."

Create a new key and paste it into the PAPERTRAIL_API_KEY script property.

"This API key has reached its daily limit."

The next scheduled refresh after midnight UTC will work. If other reports share the key, give each its own.

"Exceeded maximum execution time"

Your account has too many records for Google Sheets to load in one go. Contact our Support Team and we'll help you find another way to report on it.

Google emails you about a failed trigger

Open Executions in Apps Script to see the message, then check the FAQs below.

Seeing a different message? See Reporting tools and AI connector: FAQs and messages for every message and what to do.

Anyone who can edit the sheet can open the script and see the key. Only share edit access with people you trust.


Related Articles

Still can't find the answer you need? We'd love to help!

Contact our Customer Support Team via the chat button in the lower right corner of the screen, on +44 (0)1248 719270 or at support@papertrail.io.

Did this answer your question?