In order to achieve your goal, how about the following 3 patterns.
Pattern 1:
In this pattern, how about using Properties Service? Ref When the script is run, the value can be put and retrieved using Properties Service.
In this case, at first, it is required to input the value to Properties Service.
Sample script:
var scriptProperties = PropertiesService.getScriptProperties();
// Put value.
scriptProperties.setProperty("key", "value");
// Get value.
var value = scriptProperties.getProperty("key");
Pattern 2:
In this pattern, how about using the custom file properties? Ref This can be put and retrieve the value using Drive API.
In this case, at first, it is required to input the value to the custom file properties.
Sample script:
var fileId = ScriptApp.getScriptId();
// Put value.
Drive.Files.update({properties: [{key: "key", value: "value"}]}, fileId);
// Get value.
var savedKey = "key";
var obj = Drive.Files.get(fileId).properties.filter(({key}) => key == savedKey);
if (obj.length == 0) throw new Error("No property for the inputted key");
var value = obj[0].value;
Pattern 3:
In this pattern, how about using a temporal file? In this case, this can be used like .env file
. At first, the value is put to the file (in this case, I think that the text file and Google Docs file can be used.). And, when the script is run, the value is retrieved from the file and use it in the script.
Sample script:
// Put value.
DriveApp.createFile("sample.txt", "value");
// Get value.
var value = DriveApp.getFileById("###fileId###").getBlob().getDataAsString().trim();
References:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…