fx API और वेब ऐप्स
ENहिन्दी

PropertiesService: सेटिंग्स और API की सुरक्षित रखें

⏱ 10 मिनट

आप क्या सीखेंगे

  • API keys और settings code में क्यों नहीं होनी चाहिए
  • Script, user और document properties — कौन-सी कब
  • UI से और code से values सेट करना
  • size limits और properties किस काम के *नहीं* हैं

समझिए

समस्या

const API_KEY = 'sk_live_9f8a…';   // ❌ visible to anyone who can open the script
const ALERT_EMAIL = 'me@example.com'; // ❌ must edit code to change it

शीट का edit access रखने वाला कोई भी Extensions → Apps Script खोलकर key पढ़ सकता है। File की copy share करेंगे तो key भी साथ चली जाएगी। Email address जैसी settings बदलने के लिए भी code edit नहीं करना चाहिए।

तीन तरह की properties

Store किसके साथ shared किसके लिए
getScriptProperties() इस script को चलाने वाले सभी API keys, webhook tokens, global settings, counters
getUserProperties() सिर्फ़ मौजूदा user हर user की अपनी पसंद
getDocumentProperties() इस file को इस्तेमाल करने वाले सभी (bound scripts / add-ons) किसी एक spreadsheet की settings

सबसे ज़्यादा script properties काम आएँगी।

UI से values सेट करना

  1. Editor → Project Settings (gear icon)।
  2. नीचे Script Properties → Add script property तक जाएँ।
  3. Property: API_KEY, Value: आपकी key। Save।

इससे key आपके code से बाहर रहती है। Script project का edit access रखने वाले इसे फिर भी देख सकते हैं, इसलिए edit access सिर्फ़ भरोसेमंद लोगों को दें।

Code से पढ़ना और लिखना

const props = PropertiesService.getScriptProperties();

// Read
const apiKey = props.getProperty('API_KEY');
if (!apiKey) throw new Error('API_KEY is not set in Script Properties');

// Write one
props.setProperty('ALERT_EMAIL', 'store@example.com');

// Write several
props.setProperties({ GST_RATE: '0.03', CURRENCY: 'INR' });

// Read all as an object
const all = props.getProperties();
console.log(all.GST_RATE);

// Delete
props.deleteProperty('OLD_SETTING');

सारी values strings के रूप में रखी जाती हैं। पढ़ते समय बदलें: Number(props.getProperty('GST_RATE')), या objects के लिए:

props.setProperty('CONFIG', JSON.stringify({ reorderDays: 7, ccManager: true }));
const config = JSON.parse(props.getProperty('CONFIG') || '{}');

Config helper

सारी settings defaults के साथ एक जगह रखें:

function getConfig() {
  const p = PropertiesService.getScriptProperties().getProperties();
  return {
    alertEmail: p.ALERT_EMAIL || Session.getEffectiveUser().getEmail(),
    gstRate: Number(p.GST_RATE || 0.03),
    apiKey: p.API_KEY || ''
  };
}

Limits और क्या न रखें

  • हर value: लगभग 9 KB। हर store का कुल: लगभग 500 KB।
  • Properties छोटी settings, keys और counters के लिए हैं — data tables के लिए नहीं। Data शीट में रहना चाहिए।
  • बार-बार बदलने वाली values (जैसे progress pointer) ठीक हैं, लेकिन एक सेकंड में सैकड़ों बार पढ़ना/लिखना नहीं — थोड़ी देर के data के लिए CacheService लें।

मुख्य बातें

  • Secrets और settings code से बाहर रखें: Script Properties।
  • Values strings होती हैं — पढ़ते समय numbers और JSON बदलें।
  • ज़रूरी property न हो तो साफ़ error दें।

अभ्यास

अभ्यासअभ्यास 1

अपने invoice script के हर hard-coded ID (template ID, folder ID, GST rate) को Script Properties में ले जाइए और getConfig() helper से पढ़िए।

प्रश्नोत्तरी

Script चलाने वाले सभी लोगों के साथ कौन-सा store shared होता है? a) User properties b) Script properties c) Cache
Script properties
Property values किस type में रखी जाती हैं? a) Numbers b) Strings c) Arrays
Strings
PropertiesService: सेटिंग्स और API की सुरक्षित रखें · हिंदी | ExcelWalaa