fx शुरुआत
ENहिन्दी

Logger, console और डीबगिंग

⏱ 12 मिनट

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

  • console.log और Logger.log से values print करना
  • breakpoints के साथ debugger चलाना
  • error messages और Executions page पढ़ना
  • try...catch से errors संभालना

समझिए

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 इस्तेमाल करना

  1. बाईं ओर line number पर click करके breakpoint लगाएँ (बैंगनी बिंदु दिखेगा)।
  2. अपना function चुनें और Debug पर click करें (Run नहीं)।
  3. script breakpoint पर रुक जाएगा। दाएँ panel में हर variable की मौजूदा value दिखती है।
  4. एक-एक 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 करें।

अभ्यास

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

एक function लिखिए जो A1:A10 पढ़े, हर value उसके type के साथ log करे, और अगर Data नाम का tab न हो तो try...catch से साफ़ message दे।

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

breakpoint पर रुकने के लिए कौन-सा button? a) Run b) Debug c) Deploy
Debug
Cannot read properties of null (reading 'getRange') का मतलब ज़्यादातर… a) script बहुत देर चला b) sheet का नाम ग़लत है c) internet नहीं है
sheet का नाम ग़लत है
Logger, console और डीबगिंग · हिंदी | ExcelWalaa