You can update the version with a trigger on the client-side or using a webapp with the ScriptSync library, something like this:
function updateLibraryVersion(libraryId='', libraryName='', version) {
const script_id = ScriptApp.getScriptId();
const current_script_json = ScriptSync.getScriptContent(script_id);
var appsscript_json = current_script_json.files.find(file => {
return file.name === "appsscript"
});
if(appsscript_json) {
appsscript_json = JSON.parse(appsscript_json.source);
var library = appsscript_json.dependencies?.libraries?.find(lib => {
return (
lib.libraryId === libraryId
|| lib.userSymbol === libraryName
);
});
if(library) {
let new_version = version || Number(library.version) + 1;
library.version = (new_version).toString();
appsscript_json.source = JSON.stringify(appsscript_json, null, 2);
const updater = ScriptSync.assignTemplate();
updater.addFileToUserJson("appsscript", appsscript_json);
updater.viewChanges(370);
Utilities.sleep(20000); // waiting for interruption by user (if needed)
return updater.commit(); // after, apply the changes
}
}
return false;
}
function do(e) {
// ....
const libraryId = "your_library_script_id";
const libraryName = "LibraryNameUsersDefined";
const updateResult = updateLibraryVersion(libraryId, libraryName, e.version);
console.log(updateResult);
//...
}
If using a trigger:
function setTriggers() {
ScriptApp.newTrigger("t_updateVersion").timeBased()
.everyDays(1)
.atHour(1).create();
}
function t_updateVersion() {
/* Initialize your template script */
const template_script_id = "your_template_script_id";
const updater = ScriptSync.assignTemplate(template_script_id);
// template_appsscript - any file in your template (representing appsscript)
// script_appsscript - appsscript in the current user's script (use 'appsscript' file this)
const match = updater.compareFilesByContent(template_appsscript, script_appsscript);
if (!match) {
const updateResult = updateLibraryVersion(libraryId, libraryName);
// or copy the whole file (not the best idea, but as an option)
// because the user could set his own code here
const fileToCopy = 'appsscript';
updater.AddNewFile("template_appsscript", "appsscript", "json");
}
}
Also, in the best way, using these examples, you can extract a new version of the library from the template file and set it to the user script "appsscript" file.
Not tested (check it first):
/**
* ScriptSync id (from hint while text 'updater'):
* @param {MRaUrgsx725qu8gUzKJlDXtz8VMKMxn5x.ScriptSync} updater
*/
function updateLibraryVersionUpdater(updater, libraryId='', libraryName='', version) {
const script_id = ScriptApp.getScriptId();
const current_script_json = ScriptSync.getScriptContent(script_id);
var appsscript_json = current_script_json.files.find(file => {
return file.name === "appsscript"
});
if(appsscript_json) {
appsscript_json = JSON.parse(appsscript_json.source);
var library = appsscript_json.dependencies?.libraries?.find(lib => {
return (
lib.libraryId === libraryId
|| lib.userSymbol === libraryName
);
});
if(library) {
library.version = version;
appsscript_json.source = JSON.stringify(appsscript_json, null, 2);
updater.addFileToUserJson("appsscript", appsscript_json);
return updater.commit(); // apply the changes
}
}
return false;
}
/**
* ScriptSync id:
* @param {MRaUrgsx725qu8gUzKJlDXtz8VMKMxn5x.ScriptSync} updater
*/
function getLibraryVersion(updater, libraryId, libraryName) {
const template_app_json = updater.getFileFromTemplate("appsscript", sourceOnly=true);
if (template_app_json) {
template_app_json = JSON.parse(template_app_json);
var library = template_app_json.dependencies?.libraries?.find(lib => {
return (
lib.libraryId === libraryId
|| lib.userSymbol === libraryName
);
});
return library.version;
}
}
function t_updateVersion() {
var updateResult;
/* Initialize your template script and library */
const template_script_id = "your_template_script_id";
const updater = ScriptSync.assignTemplate(template_script_id);
// template_appsscript - any file in your template (representing appsscript)
// script_appsscript - appsscript in the current user's script (use 'appsscript' file this)
const match = updater.compareFilesByContent(template_appsscript, script_appsscript);
if (!match) {
const libraryVersion = getLibraryVersion(updater, libraryId, libraryName);
if (libraryVersion)
updateResult = updateLibraryVersionUpdater(updater, libraryId, libraryName, libraryVersion);
}
return updateResult || false;
}