समझिए
Calendar लेना
const cal = CalendarApp.getDefaultCalendar(); // your own calendar
// const cal = CalendarApp.getCalendarById('team@group.calendar.google.com'); // a shared one
Shared calendar का ID Google Calendar में मिलता है → Settings and sharing → Integrate calendar → Calendar ID।
Events बनाना
const start = new Date('2026-10-15T11:00:00');
const end = new Date('2026-10-15T11:30:00');
cal.createEvent('Customer visit: Rahul', start, end, {
description: 'Ring resizing, bring old ring',
location: 'Karol Bagh store',
guests: 'rahul@example.com',
sendInvites: false
});
cal.createAllDayEvent('Annual stock audit', new Date(2026, 10, 6)); // months start at 0 → 6 Nov 2026
शीट
Tab Appointments:
| A Customer | B Email | C Date | D Time | E Duration (min) | F Purpose | G Event ID |
|---|
Date और time cells जोड़ना
Sheets सिर्फ़ समय वाले cell (जैसे 11:30) को 30-Dec-1899 की तारीख़ के साथ उस समय के रूप में रखता है। दोनों cells को ऐसे जोड़ें:
function combine(dateCell, timeCell) {
const d = new Date(dateCell);
const t = new Date(timeCell);
d.setHours(t.getHours(), t.getMinutes(), 0, 0);
return d;
}
Script
function syncAppointments() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Appointments');
const lastRow = sheet.getLastRow();
if (lastRow < 2) return;
const cal = CalendarApp.getDefaultCalendar();
const rows = sheet.getRange(2, 1, lastRow - 1, 7).getValues();
const ids = [];
rows.forEach(([customer, email, date, time, minutes, purpose, eventId]) => {
if (!customer || !date || !time) { ids.push([eventId]); return; }
const start = combine(date, time);
const end = new Date(start.getTime() + (Number(minutes) || 30) * 60 * 1000);
const title = `${purpose || 'Appointment'}: ${customer}`;
let event = eventId ? cal.getEventById(eventId) : null;
if (event) {
// Update the existing event instead of creating a duplicate
event.setTitle(title);
event.setTime(start, end);
} else {
event = cal.createEvent(title, start, end, {
description: `Customer email: ${email}`,
location: 'Store'
});
}
ids.push([event.getId()]);
});
sheet.getRange(2, 7, ids.length, 1).setValues(ids);
}
हर run मौजूदा events को update करता है और सिर्फ़ नए events बनाता है, क्योंकि Event ID column याद रखता है कि कौन-सी row किस event की है।
Reminders
event.removeAllReminders();
event.addPopupReminder(60); // 60 minutes before
event.addEmailReminder(24 * 60); // 1 day before
Time zones
तारीख़ें script के time zone (Project Settings) पर चलती हैं। अगर events ग़लत घंटे पर दिखें, तो जाँचें कि script और calendar दोनों India Standard Time (या आपके zone) पर हैं।
मुख्य बातें
createEvent(title, start, end, options)औरcreateAllDayEvent(title, date)।- Event बनाने से पहले अलग date और time cells जोड़ें।
- Duplicate नहीं, update करने के लिए event IDs save करें।