So what I did previously is when I make functions to call them, I would make variables of the same value, which is var sheet = SpreadsheetApp.openById('id').getActiveSheet();
, and someone suggested/required me to save the sheet as a named property instead and keep calling it in the functions instead of making like 100 of same variables with the exact same values.
I watched the videos on Youtube and stuff - tried this out:
var documentProperties = PropertiesService.getDocumentProperties();
var sheetID = "1dRAXZRHMgfR_oX1ZF1RFeonUi7ZPk_wbRf7Jx0UvCjE";
documentProperties.setProperties('spreadsheetID', sheetID);
var sheet = SpreadsheetApp.openById(documentProperties.getProperty(spreadsheetID)).getActiveSheet();
var logsheet = SpreadsheetApp.openById(documentProperties.getProperty(spreadsheetID)).getSheetByName("Log");
documentProperties.setProperties('timesheet', sheet);
documentProperties.setProperties('logsheet',log);
So I need to be able to call the variables sheet and logsheet in all other functions - if I have these at function onOpen()
, will I be able to do that by just simply using documentProperties.getProperties(timesheet)
or documentProperties(logsheet)
?
Short answer is no, you can't call them as a function
documentProperties(logsheet)
. The correct way isdocumentProperties.getProperty('logsheet')
.You can use them as some kind of global variables, but you are using the wrong methods:
SetProperties(), in plural, is to set multiple properties:
Keep in mind that Properties cannot be shared between scripts, so you can only use those within the scripts of the same project, but yes, you can use them in an
onOpen()
function as any regular variable. To make sure you are getting the values you want, put aLogger.log(documentProperties.getProperties())
at the end of the code.