समझिए
समस्या
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 सेट करना
- Editor → Project Settings (gear icon)।
- नीचे Script Properties → Add script property तक जाएँ।
- 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 दें।