← Integrations

No-code / CMS · JavaScript

Panchang in Google Sheets with Apps Script

A spreadsheet is often where a temple office or a content team plans the month. Apps Script lets a cell call AstroNode: =PANCHANG(A2, 330, 28.6139, 77.209, "tithi"). The code is short; what matters is caching, the 30-second limit on custom functions, and how a date cell reaches your script.

Updated

How this page was checked: The script was executed on Node 24 with the Apps Script globals stubbed: UrlFetchApp.fetch as a synchronous curl call to the in-memory API, PropertiesService, CacheService (an in-memory map), Utilities.formatDate and SpreadsheetApp. Google's runtime, its quotas and the trigger were not exercised.

Put the key in Script Properties

Open Extensions → Apps Script, then Project Settings and add a script property ASTRONODE_API_KEY. Script properties are not cells: people who can only view or comment on the spreadsheet cannot read them. Anyone with edit access to the spreadsheet can open its bound script project, though, so share edit rights accordingly - and never put the key in a cell or a formula argument, where every viewer sees it.

The script

Code.gs:

js
const ASTRONODE = 'https://api.astronode.dev';

function astronodePost_(path, body) {
  const key = PropertiesService.getScriptProperties().getProperty('ASTRONODE_API_KEY');
  const res = UrlFetchApp.fetch(ASTRONODE + path, {
    method: 'post',
    contentType: 'application/json',
    headers: { Authorization: 'Bearer ' + key },
    payload: JSON.stringify(body),
    muteHttpExceptions: true, // read the error body instead of a bare exception
  });
  const json = JSON.parse(res.getContentText());
  if (res.getResponseCode() !== 200) {
    throw new Error(json.error.code + ': ' + json.error.message);
  }
  return json.data;
}

/**
 * One panchang field for a date and place, e.g. =PANCHANG(A2, 330, 28.6139, 77.209, "tithi").
 * @param {Date} date A date cell.
 * @param {number} tzOffsetMinutes East-positive offset of the place, 330 for India.
 * @param {number} lat Latitude.
 * @param {number} lng Longitude.
 * @param {string} field tithi, nakshatra, yoga, karana, sunrise or sunset.
 * @customfunction
 */
function PANCHANG(date, tzOffsetMinutes, lat, lng, field) {
  // A date cell arrives as a JavaScript Date; the API wants YYYY-MM-DD.
  const sheetZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
  const day = Utilities.formatDate(date, sheetZone, 'yyyy-MM-dd');

  const cache = CacheService.getScriptCache();
  const cacheKey = ['panchang', day, tzOffsetMinutes, lat, lng].join(':');
  let data = JSON.parse(cache.get(cacheKey) || 'null');
  if (!data) {
    data = astronodePost_('/v1/panchang', { date: day, tz_offset_minutes: tzOffsetMinutes, lat: lat, lng: lng });
    cache.put(cacheKey, JSON.stringify(data), 21600); // 6 hours, the maximum
  }

  if (field === 'tithi') return data.tithi.paksha + ' ' + data.tithi.name;
  if (field === 'sunrise' || field === 'sunset') {
    // UTC instant -> wall-clock time at the place, not in the sheet's zone.
    const local = new Date(Date.parse(data[field]) + tzOffsetMinutes * 60000);
    return Utilities.formatDate(local, 'UTC', 'HH:mm');
  }
  return data[field].name;
}

/** Time-driven trigger: append today's panchang for one city as a new row. */
function logTodaysPanchang() {
  const tz = 330, lat = 28.6139, lng = 77.209; // New Delhi
  const today = Utilities.formatDate(new Date(), 'Asia/Kolkata', 'yyyy-MM-dd');
  const p = astronodePost_('/v1/panchang', { date: today, tz_offset_minutes: tz, lat: lat, lng: lng });
  SpreadsheetApp.getActive().getSheetByName('Panchang').appendRow([
    p.date, p.vara.name, p.tithi.paksha + ' ' + p.tithi.name, p.nakshatra.name, p.yoga.name, p.karana.name,
    p.sunrise, p.sunset,
  ]);
}

muteHttpExceptions: true is what lets the function show AstroNode's error code in the cell (#ERROR! with the message on hover) instead of Apps Script's generic fetch failure. The @customfunction tag puts PANCHANG in the formula autocomplete with its parameter help.

In the sheet

`A2` holds the date 5 December 2026; spreadsheet time zone Asia/Kolkata
FormulaResult
=PANCHANG(A2, 330, 28.6139, 77.209, "tithi")Krishna Dwadashi
=PANCHANG(A2, 330, 28.6139, 77.209, "nakshatra")Chitra
=PANCHANG(A2, 330, 28.6139, 77.209, "yoga")Saubhagya
=PANCHANG(A2, 330, 28.6139, 77.209, "karana")Kaulava
=PANCHANG(A2, 330, 28.6139, 77.209, "sunrise")06:59 (the API's 2026-12-05T01:29:14.000Z)
=PANCHANG(A2, 330, 28.6139, 77.209, "sunset")17:23

Six cells, one API call in the run: the first cell fetched and cached the whole panchang, the other five read the cache. In Sheets, cells can recalculate in parallel, so a cold cache may cost a few calls rather than one - still far from one per cell. A panchang for a given date and place never changes, so the six-hour cache cannot serve a stale answer.

For a log instead of a formula, add a trigger (the clock icon in the editor): function logTodaysPanchang, time-driven, day timer. It appended 2026-12-05, Shanivara, Krishna Dwadashi, Chitra, Saubhagya, Kaulava and the two UTC instants. Run it once from the editor first so Google asks for the fetch permission. The /v1/panchang endpoint is on the Free plan; which sunrise convention it uses is in panchang sunrise conventions.

Apps Script limits that shape the design

  • 30 seconds per custom function call. Each cell is its own execution. One API call fits easily; a loop over a month inside one formula does not - use the trigger, which gets the longer script time limit, for bulk work.
  • Custom functions can read script properties but not set them, and cannot show authorisation prompts. Reading the key and calling UrlFetchApp both work, which is all this needs.
  • URL Fetch has a daily quota per account, published on Google's Apps Script quotas page. A sheet of 365 dates × 5 fields is 1,825 calls without the cache and at most 365 with it.
  • Cache entries live at most 21,600 seconds (6 hours) and are not guaranteed to last that long; treat the cache as a saving, not as storage. For a permanent record, use the trigger and write rows.

The mistake that bites first: the date cell

A date cell reaches a custom function as a JavaScript Date. Concatenating it or calling String(date) gives text like Sat Dec 05 2026 00:00:00 GMT+0530 (India Standard Time), and the API answered that with 400 VALIDATION_ERROR: date must be ISO YYYY-MM-DD. Format it with Utilities.formatDate - and in the spreadsheet's time zone (getSpreadsheetTimeZone()), because the cell holds midnight in that zone. Formatting in the script project's zone, if the two differ, can move the date back a day.

The same care applies on the way out: sunrise is a UTC instant. Formatting it in the spreadsheet's zone gives the right clock time only when the sheet and the place share a zone; adding the place's own offset, as the script does, is right for any city in the list.

Questions

Which AstroNode endpoints does the Google Sheets example call?
1 endpoint: Panchang (POST /v1/panchang).
Was the Google Sheets code on this page actually run?
The script was executed on Node 24 with the Apps Script globals stubbed: UrlFetchApp.fetch as a synchronous curl call to the in-memory API, PropertiesService, CacheService (an in-memory map), Utilities.formatDate and SpreadsheetApp. Google's runtime, its quotas and the trigger were not exercised.