I have this global variable in my script:
var targetDocId = 'No doc ID informed yet';
I'm trying to change the value of this global variable inside my main function:
function generatePersonDatasheet() {
var target = createDuplicateDocument(template, docName);
var link = target.getUrl();
targetDocId = target.getId(); // <-------- HERE
if (theses == 1){
Logger.log("going to showList() ");
return showList();
}
showURL(docName, link);
}
After that, I'm trying to acess the changed global variable value in a handler function:
function submit(e){
var numberOfItems = Number(e.parameter.checkbox_total);
var thesesArrays = [];
for(var i = 0; i < numberOfItems; i++){
if(e.parameter['checkbox_isChecked_'+i] == 'true'){
thesesArrays.push(fact_list[i]);
}
}
for (var i = 0; i < thesesArrays.length; i++){
var thesesId = thesesArrays[i][2];
var thesesType = thesesArrays[i][1];
importTheses(targetDocId, thesesId, thesesType); // <-----HERE
}
return UiApp.getActiveApplication().close();
}
function importTheses(targetDocId, thesesId, thesesType) {
var targetDoc = DocumentApp.openById(targetDocId);
var targetDocParagraphs = targetDoc.getParagraphs();
var targetDocElements = targetDocParagraphs.getNumChildren();
var thesesDoc = DocumentApp.openById(thesesId);
var thesesParagraphs = thesesDoc.getParagraphs();
var thesesElements = thesesDoc.getNumChildren();
var eltargetDoc=[];
var elTheses=[];
for( var j = 0; j < targetDocElements; ++j ) {
var targetDocElement = targetDoc.getChild(j);
eltargetDoc[j]=targetDocElement.getText();
if(el[j]== thesesType){
for( var k = 0; k < thesesParagraphs-1; ++k ) {
var thesesElement = thesesDoc.getChild(k);
elTheses[k] = thesesDoc.getText();
targetDoc.insertParagraph(j, elTheses[k]);
}
}
}
}
But, as long as I tryied to change the targetDocId
, when it's use as argument to importTheses(targetDocId, thesesId, thesesType);
it still has the value 'No doc ID informed yet', even I have changed it, as if the program had been run from the beginning. Is an alternative to this "reset to original value" behavior? Or I have to use scriptDB or ScriptProperties to store the changed value of the global variable?