I set up a simple script to download a CSV into a Google Sheet. I saved the code, gave permissions on running, ran it from the script editor associated with the correct sheet, double and triple checked that the sheet name is consistent between script and sheet...and still I get an error saying that the sheet cannot be found when I run the script, even though I am in that account and looking at it. It's driving me nuts. Has anyone encountered this, or do you have any suggestions?
// URL of the CSV file containing hourly dataconst CSV_URL = "https://mysource.com/data.csv";// Name of your Google Sheet where the data will be importedconst SHEET_NAME = "datafeed";function fetchDataAndUpdateSheet() { try { // Fetch data from the CSV URL const response = UrlFetchApp.fetch(CSV_URL); const csvData = Utilities.parseCsv(response.getContentText()); // Get the sheet object const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME); // Check if sheet exists and handle potential error if (!sheet) { throw new Error("Sheet not found: " + SHEET_NAME); } // Clear existing data (optional, adjust as needed) sheet.clearContents(); // Write fetched data to the sheet sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData); Logger.log("Data fetched from '" + CSV_URL +"' and sheet updated successfully!"); } catch (error) { Logger.log("Error: " + error.message); // Optional: Send email notification on error (requires setting up a trigger) // ... (code to send email notification) }}// Set up a trigger to run the function hourlyfunction createTrigger() { ScriptApp.newTrigger('fetchDataAndUpdateSheet') .timeBased() .everyHours(1) .create();}// Run the trigger creation function on script loadfunction onOpen() { createTrigger();}Execution log2:51:02 PM Notice Execution started2:51:05 PM Info Error: Sheet not found: datafeed2:51:04 PM Notice Execution completed