Suddenly encountering an error in Google Sheets function that worked fine before. Any recent changes?

I’m having trouble with a function I made a while back to check if a sheet exists in a workbook. Here’s what it looks like:

function isSheetPresent(sheetName) {
  var book = getCurrentWorkbook();
  var sheet = book.getSheetByName(sheetName);
  return sheet !== null && sheet.getName() === sheetName;
}

function getCurrentWorkbook() {
  return SpreadsheetApp.getActive();
}

For some reason, it started throwing a TypeError about a month ago. The error says something about not being able to use ‘toString’ on null. I’m pretty sure I didn’t change anything in the script.

Has anyone else run into this? I’m wondering if Google made some changes on their end without telling us. It’s frustrating because this worked perfectly for ages. Any ideas on what might be causing this or how to fix it?

yo, i’ve been dealing with similar bs lately. google’s always messing with stuff behind the scenes. try adding a delay before checking the sheet name, like:

function isSheetPresent(sheetName) {
  var book = getCurrentWorkbook();
  var sheet = book.getSheetByName(sheetName);
  Utilities.sleep(100);
  return sheet !== null && sheet.getName() === sheetName;
}

sometimes that helps when google’s being slow. worth a shot, right?

I’ve encountered similar issues with Google Sheets functions recently. It’s possible Google made some backend changes that affected script behavior. Have you tried debugging step-by-step? One potential fix is to add a null check before accessing sheet properties:

function isSheetPresent(sheetName) {
  var book = getCurrentWorkbook();
  var sheet = book.getSheetByName(sheetName);
  return sheet !== null && sheet !== undefined && sheet.getName() === sheetName;
}

This extra check might prevent the TypeError. If that doesn’t work, consider logging intermediate values to pinpoint where the error occurs. It’s also worth checking if your script has the necessary permissions after any recent Google updates. Hope this helps troubleshoot the issue!

I’ve run into similar frustrations with Google Sheets lately. It’s maddening when something that’s worked flawlessly for ages suddenly breaks without warning. In my experience, these issues often stem from subtle changes in how Google handles null or undefined values.

One thing that’s helped me is wrapping potentially problematic function calls in try/catch blocks. This way, you can gracefully handle errors and potentially log them for debugging. Here’s how you might modify your function:

function isSheetPresent(sheetName) {
  try {
    var book = getCurrentWorkbook();
    var sheet = book.getSheetByName(sheetName);
    return sheet != null && sheet.getName() === sheetName;
  } catch (error) {
    console.error('Error in isSheetPresent:', error);
    return false;
  }
}

This approach has saved me countless headaches when dealing with unexpected Google Sheets behavior. It might not solve the root cause, but it should at least prevent the script from crashing and give you more insight into what’s going wrong.