समझिए
Values print करना
function checkValues() {
const sheet = SpreadsheetApp.getActiveSheet();
const value = sheet.getRange('B2').getValue();
console.log('B2 contains:', value);
console.log('Type is:', typeof value);
}
इसे चलाइए और editor के नीचे Execution log panel देखिए।
console.log— आज का तरीका। यह Executions page पर भी लिखता है, इसलिए trigger से चले runs के logs बाद में भी दिखते हैं। समस्याओं के लिएconsole.warn/console.errorलें।Logger.log— पुराना तरीका; सिर्फ़ मौजूदा run के execution log में दिखता है।
Tip: typeof value print करने से बहुत-से bugs हल हो जाते हैं। जिस cell को आप number समझ रहे हैं उसमें असल में text '1,200' हो सकता है।
Debugger इस्तेमाल करना
- बाईं ओर line number पर click करके breakpoint लगाएँ (बैंगनी बिंदु दिखेगा)।
- अपना function चुनें और Debug पर click करें (Run नहीं)।
- script breakpoint पर रुक जाएगा। दाएँ panel में हर variable की मौजूदा value दिखती है।
- एक-एक line चलाने के लिए Step over, आगे बढ़ने के लिए Resume।
जब कोई loop ग़लत नतीजा दे और आपको row-दर-row values बदलती देखनी हों, तब debugger काम आता है।
Error messages पढ़ना
एक आम error:
TypeError: Cannot read properties of null (reading 'getRange')
at readStock (Code:3:24)
- क्या हुआ: आपने किसी
nullचीज़ पर.getRangeचलाया। - कहाँ: file
Code, line 3। - आम वजह:
getSheetByName('Stock')नेnullलौटाया क्योंकि tab का नाम अलग है (जैसे'stock'या space के साथ'Stock ')।
आम errors और उनकी आम वजहें:
| Error | आम वजह |
|---|---|
Cannot read properties of null |
sheet/range का नाम मौजूद नहीं |
The number of rows in the data does not match the number of rows in the range |
setValues के array का size ≠ range का size |
Exception: You do not have permission to call ... |
simple trigger या custom function से Gmail/Drive चलाना |
Exceeded maximum execution time |
script ~6 मिनट से ज़्यादा चला |
ReferenceError: x is not defined |
variable या function के नाम में typo |
Executions page
बाईं ओर Executions icon (list) पर click करें। हर run — manual, trigger या web app से — यहाँ दिखता है, उसके status (Completed / Failed), समय और logs के साथ। रात 3 बजे कोई scheduled trigger fail हो जाए तो वजह यहीं मिलेगी।
try...catch से errors संभालना
function safeRead() {
try {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Stock');
if (!sheet) {
throw new Error('Tab "Stock" not found');
}
const qty = sheet.getRange('B2').getValue();
console.log('Qty:', qty);
} catch (err) {
console.error('Something went wrong: ' + err.message);
// Optionally email yourself:
// MailApp.sendEmail('you@example.com', 'Script error', err.stack);
}
}
Trigger से चलने वाले scripts में try...catch लगाइए ताकि failure चुपचाप न हो, बल्कि साफ़ log हो (या आपको email आ जाए)।
मुख्य बातें
console.log+ Executions page = debugging के मुख्य औज़ार।- पहले error की line number पढ़ें, फिर नाम और data types जाँचें।
- Scheduled jobs को
try...catchमें लपेटें और errors report करें।