I have a serious problem when using Libraries in Google Sheets.
I have two Spreadsheets: Dashboard1 and Dashboard2. In both spreadsheets, I have multiple scripts and Triggers.
There is one trigger in specific that triggers on event OnEdit in Dashboard2. Here is the code:
Dashboard2
Code.gs
function onEdit(e){
Dashboard1.updateReferral(e.oldValue, e.value);
}
Dashboard1
Code.gs
function updateReferral(evaluatedEmail, newReferral){
var employee = returnEmployeeRow(evaluatedEmail);
PAYROLL_SHEET.getRange(REFERRAL_COLUMN + employee[2]).setValue(newReferral);
}
Constants.gs
var SHEET_CATEGORIES = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Categorias");
var RANGE_CATEGORIES = SHEET_CATEGORIES.getRange("A2:G");
var CATEGORIES = RANGE_CATEGORIES.getValues();
What I want to do is when someone edit in Dashboard2, it updates a single row in Dashboard1.
When I tried to trigger the event, it throws the error
TypeError: Cannot call method "getRange" of null. (line 2, file "Constants", project "XXXXXX")
I figured out that this error exist because there is no sheet "Categorias" in Dashboard2. If I create a sheet "Categorias" in Dashboard2, it works fine.
My question is:
This is a normal behavior? Why when I run the code, it tries to initialize the variable from Constants.gs in Dashboard1 as they exist in Dashboard2?
There is a solution for this? Or just I can't do this?
I've tried to use different name in the .gs files but it didn't work. Also tried to delete the file Constants.gs and have all the variables in Code.gs.