Append an Object as a Sheet Row

When your data is already a plain object (API payload, form values), aligning it to row-1 headers beats hand-building arrays. Missing keys become blank cells so column order stays stable.

What you'll need

  • A sheet whose first row is headers matching your object keys
  • A plain object (not nested deeply — flatten first if needed)

How to use this snippet

function appendObjectAsRow(sheet, obj) { var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]; var row = headers.map(function (h) { return obj.hasOwnProperty(h) ? obj[h] : ''; }); sheet.appendRow(row); return sheet.getLastRow(); } function logSignup(user) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Signups'); return appendObjectAsRow(sheet, { timestamp: new Date(), name: user.name, email: user.email }); }

Tips:

  • Header names must match object keys exactly (including case).
  • Pair with rowsToObjects when you read the same sheet back as objects.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free themes and snippets — plus optional Co-Pilot when you want a boost (1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension