Google Apps Script Optimization: An AI Agent Skill for Clean Code

After using AI extensively to generate and refactor Google Apps Script code, I noticed a recurring problem: the code often works, but it is not always written with Apps Script performance in mind.

Although Google provides official Apps Script documentation, the ecosystem has fewer practical resources and examples than many mainstream JavaScript platforms. AI-generated scripts can therefore contain unnecessary service calls, inefficient spreadsheet operations or code that becomes difficult to maintain as the project grows.

Rather than repeating the same instructions in every prompt, I created google-apps-script-standards, an open-source Agent Skill that helps AI coding agents write clean, optimized and maintainable Google Apps Script code. It brings together the development practices I use in my own projects.

What is an Agent Skill?

An Agent Skill is a reusable set of instructions, typically organized around a SKILL.md file. It gives a coding agent specific development guidelines without requiring you to repeat them in every prompt. It is not an Apps Script library and does not run inside your script.

Google Apps Script performance and coding best practices

  • Code structure: centralized configuration, consistent entry-point naming and typed JSDoc for editor autocomplete.
  • Google Sheets performance: batch operations, column lookup by header and targeted writes that avoid overwriting unrelated cells.
  • Long-running scripts: checkpoints, retries that account for quotas and structured Cloud Logging summaries.
  • Web app performance: lightweight initial payloads and fewer client-to-server calls through grouped google.script.run requests.

The objective is to make generated code more efficient, maintainable and suitable for the Apps Script execution environment.

How to install the Google Apps Script Agent Skill

The skill is available on GitHub: Google Apps Script Standards.

With Node.js 22.20 or later installed, open a terminal in your local Apps Script project and run:

npx skills add lidia-rbr/google-apps-script-skills --skill google-apps-script-standards

Select your coding agent when prompted. Alternatively, download the repository and copy the entire skills/google-apps-script-standards directory into your agent’s skills directory. The location depends on the agent you use.

How to optimize Google Apps Script code with AI

Once installed, ask your coding agent to apply the skill to an existing project or a new implementation. For example:

Use google-apps-script-standards to optimize my Apps Script code. Batch spreadsheet operations, identify columns by header and preserve existing formulas outside the target columns.

A common example is slow Google Sheets automation. Calling getValue() or setValue() repeatedly inside a loop creates unnecessary calls to the spreadsheet service. Where appropriate, the agent should read the data with getValues(), process it in JavaScript arrays or maps, and write the results with setValues(). This reduces service calls without changing the intended result. For targeted updates, it should also avoid overwriting unrelated cells or formulas.

Review the proposed changes and test them before deploying. The skill provides development guidance; it does not guarantee performance improvements or correctness.

Use the skill with VS Code and clasp

If you are still developing directly in the Apps Script editor, follow my guide to Google Apps Script in VS Code with clasp to set up a local development environment where you can work with coding agents and reusable skills.

You can also explore Google’s Apps Script best practices for more information about optimizing service calls.

Solving Slow Google Sheets: Boost Performance with Smart Tricks and Automation

Google Sheets is amazing, until it isn’t. As your spreadsheets grow with formulas, collaborators, and live data, they can get painfully slow. Here are all the tricks I’ve found to help my clients get better performances.

Why Google Sheets Get Slow

  • Too many formulas, especially volatile ones (NOW(), TODAY(), RAND())
  • Large datasets (thousands of rows/columns)
  • Repeated or redundant calculations
  • Overuse of heavy formulas or IMPORTRANGE, ARRAYFORMULA, QUERY…
  • Dozens of collaborators editing simultaneously
  • Conditional formatting or custom functions on big ranges

Quick Fixes to Speed Things Up

✔️ Tidy Up Your Formulas

Start by removing or replacing volatile functions like NOW() or RAND() with static values when you don’t need them to update constantly. Use helper columns to calculate something once and reference that instead of repeating logic across rows.

Use fixed ranges (A2:A1000) instead of full-column references. Every formula referencing A:A makes Google Sheets look at all 50,000+ possible rows, even if you’re only using 1,000.

✔️ Lighten the Load

Think of your sheet as a backpack: if it’s stuffed with years of unused data and unnecessary features, it’ll be slow to move. Delete unused rows and columns. Archive old data to separate files. Keep the main spreadsheet focused on what matters now.

✔️ Be Visual, Not Flashy

Conditional formatting, while beautiful, is a silent performance killer. Use it selectively and avoid applying rules to entire columns or rows. Likewise, go easy on charts and embedded images. Move your dashboards to a separate file if needed.

Apps Script and Heavy-Duty Sheets

When dealing with large spreadsheets, your scripting strategy matters. Google Apps Script offers two main interfaces: SpreadsheetApp and the advanced Sheets API. While SpreadsheetApp is easier to use, it becomes painfully slow with big datasets because it handles operations cell by cell.

Instead, for serious performance, we lean heavily on the Sheets API and its batchUpdate method. This allows us to push changes to many cells at once, minimizing round trips and drastically speeding up execution.

Here’s a simple example that injects data into a large range using the Sheets API, not SpreadsheetApp:

// Example of data
const data = [{
  "range": "A35",
  "values": [["TEST"]]
}];

/**
 * Write to multiple, disjoint data ranges.
 * @param {string} spreadsheetId The spreadsheet ID to write to.
 * @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate
 */
function writeToMultipleRanges(spreadsheetId, data, sheetName) {

  let ssGetUrl = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}`
  let options = {
    muteHttpExceptions: false,
    contentType: 'application/json',
    method: 'get',
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() }
  };
  let ssGetresponse = JSON.parse(UrlFetchApp.fetch(ssGetUrl, options));
  let sheets = ssGetresponse.sheets;
  let rowCount = 0;
  let sheetId = 0;
  sheets.forEach(sheet => {
    if (sheet.properties.title == sheetName) {
      rowCount = sheet.properties.gridProperties.rowCount
      sheetId = sheet.properties.sheetId;
    }
  });

  let num = parseInt(String(data[0]["range"]).split("!")[1].replace(/[^0-9]/g, ''));
  if (rowCount < num) {
    let diff = num - rowCount;
    let resource = {
      "requests": [
        {
          "appendDimension": {
            "length": diff,
            "dimension": "ROWS",
            "sheetId": sheetId
          }
        }
      ]
    };
    let ssBatchUpdateUrl = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}:batchUpdate`
    let options = {
      muteHttpExceptions: true,
      contentType: 'application/json',
      method: 'post',
      payload: JSON.stringify(resource),
      headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() }
    };
    let response = JSON.parse(UrlFetchApp.fetch(ssBatchUpdateUrl, options));
  }

  const request = {
    'valueInputOption': 'USER_ENTERED',
    'data': data
  };

  try {
    const response = Sheets.Spreadsheets.Values.batchUpdate(request, spreadsheetId);
    if (response) {
      return;
    }
    console.log('response null');
  } catch (e) {
    console.log('Failed with error %s', e.message);
  }
}

/**
 * Converts a row and column index to A1 notation (e.g., 3, 5 → "E3").
 *
 * @param {number} rowIndex - The 1-based row index.
 * @param {number} colIndex - The 1-based column index.
 * @returns {string} The A1 notation corresponding to the given indices.
 */
function getA1Notation(rowIndex, colIndex) {
  if (rowIndex < 1 || colIndex < 1) {
    throw new Error("Both rowIndex and colIndex must be greater than or equal to 1.");
  }
  let columnLabel = "";
  let col = colIndex;
  while (col > 0) {
    const remainder = (col - 1) % 26;
    columnLabel = String.fromCharCode(65 + remainder) + columnLabel;
    col = Math.floor((col - 1) / 26);
  }
  return columnLabel + rowIndex;
}

Freeze Formulas

Often, you only need a formula to calculate once, not every time the sheet updates.

Instead of leaving it active and recalculating endlessly, you can use Apps Script to compute the values once and then lock them in as static data.

The script below is designed for that: it keeps the original formulas in the second row (as a reference and for future recalculations), then copies those formulas down the column, computes all results, and finally replaces everything below the second row with raw values. The result? A faster sheet that still preserves its logic blueprint in row two.

/**
 * 
 */
function Main_Update_Formula_Data(ssId, sheet) {

  // Get formulas in first row
  const formulas = sheet.getRange(2, 1, 1, sheet.getLastColumn()).getFormulas()[0];

  // Get last row index
  const lastRowIndex = sheet.getLastRow();

  // Prepare ranges to treat
  const ranges = [];

  // Loop through formulas to expand until the end
  for (let i = 0; i < formulas.length; i++) {

    if (formulas[i] != ""
      && !String(formulas[i]).startsWith("=arrayformula")
      && !String(formulas[i]).startsWith("=ARRAYFORMULA")
      && !String(formulas[i]).startsWith("=ArrayFormula")) {

      const thisRange = {
        startRowIndex: 1,
        endRowIndex: lastRowIndex - 1,
        startColumnIndex: i,
        endColumnIndex: i + 1,
        formulas: [formulas[i]],
      };

      ranges.push(thisRange);
    }
  }

  // Inject and freeze data except first row of formulas
  injectAndFreezeFormulas(ssId, sheet.getSheetId(), sheet.getName(), ranges);
}


/**
 * Injects formulas into multiple ranges, waits for computation, then replaces them with static values.
 * @param {string} spreadsheetId - Spreadsheet ID.
 * @param {number} sheetId - Numeric sheet ID.
 * @param {string} sheetName - Sheet name (for value reads/writes).
 * @param {Array<Object>} ranges - Array of range configs like:
 * @return {void}
 */
function injectAndFreezeFormulas(spreadsheetId, sheetId, sheetName, ranges) {
  // --- Inject formulas via batchUpdate
  const requests = [];
  ranges.forEach(rangeDef => {
    const {
      startRowIndex,
      endRowIndex,
      startColumnIndex,
      endColumnIndex,
      formulas,
    } = rangeDef;

    formulas.forEach((formula, idx) => {
      requests.push({
        repeatCell: {
          range: {
            sheetId,
            startRowIndex,
            endRowIndex,
            startColumnIndex: startColumnIndex + idx,
            endColumnIndex: startColumnIndex + idx + 1,
          },
          cell: {
            userEnteredValue: { formulaValue: formula },
          },
          fields: 'userEnteredValue',
        },
      });
    });
  });

  Sheets.Spreadsheets.batchUpdate({ requests }, spreadsheetId);
  Logger.log(`Formulas injected into ${ranges.length} range(s)`);

  // --- Wait briefly to ensure calculations are done
  Utilities.sleep(1000);

  // --- Read computed values via API (valueRenderOption: UNFORMATTED_VALUE)
  const readRanges = ranges.map(r =>
    `${sheetName}!${columnLetter(r.startColumnIndex + 1)}3:` +
    `${columnLetter(r.endColumnIndex)}${r.endRowIndex + 1}`
  );

  const readResponse = Sheets.Spreadsheets.Values.batchGet(spreadsheetId, {
    ranges: readRanges,
    valueRenderOption: 'UNFORMATTED_VALUE',
  });

  // --- Reinject values (overwrite formulas)
  const writeData = readResponse.valueRanges.map((vr, idx) => ({
    range: readRanges[idx],
    values: vr.values || [],
  }));

  Sheets.Spreadsheets.Values.batchUpdate(
    {
      valueInputOption: 'RAW',
      data: writeData,
    },
    spreadsheetId
  );

  Logger.log(`Formulas replaced by static values in ${writeData.length} range(s)`);
}

/**
 * Helper — Convert a column index (1-based) to A1 notation.
 *
 * @param {number} colIndex - 1-based column index.
 * @return {string}
 */
function columnLetter(colIndex) {
  let letter = '';
  while (colIndex > 0) {
    const remainder = (colIndex - 1) % 26;
    letter = String.fromCharCode(65 + remainder) + letter;
    colIndex = Math.floor((colIndex - 1) / 26);
  }
  return letter;
}

Did you know all of these tricks? If you’ve got a favorite performance tip, clever automation, or formula hack, drop it in the comments! Let’s keep our Sheets smart, fast, and fun to work with.

How to Push Big Datasets into Google Sheets with Apps Script (Without Crashing!)

Copy-pasting big datasets manually into Google Sheets can quickly turn into a nightmare: slowdowns, crashes, weird formatting issues… you name it.

But guess what? With a little bit of Google Apps Script magic, you can automate the entire process, safely and efficiently.

In this guide, I’ll show you how to push large amounts of data into your sheet like a pro, without risking a browser meltdown.

(And don’t worry, I’ll keep it simple and copy-paste ready.)