Deze functies worden gebruikt om de credential file te lezen tijdens de eerste configuratie.
Deze functie vormt de File URL om naar een File ID, bruikbaar in andere code.
/**
* Get file ID from URL
* @param{string} url of file
* @return {string} file ID
*/
function getFileIdFromDriveUrl(url) {
var match = url.match(/([a-z0-9_-]{25,})[$/&?]/i);
return match ? match[1] : null;
}
Dit is de functie die de credentials zal lezen om toegang te hebben tot de FS (of andere API's).
De waardes worden in deze functie toegekend aan Document Properties, zodat dit slechts één maal dient te gebeuren.
Door de toegang tot de file te beperken tot iemand met verhoogde rechten, verhinder je dat anderen de credentials in handen krijgen.
Eens de data in de document properties zit (variabelen) is het onmogelijk om deze te bekomen.
/**
* Read JSON key file and copy key info to Scrip properies
* Use: Key is stored as a private document.
* who needs the Apps script can copy the code and requests the author access to the file.
* He will get a link to the file, that has to be past into the userproperties of the script.
* through a user dialog.
* @param {string} URL to keyfile
* @return {bool} true of ok
*/
function readKeyFile(fileURL) {
try {
var files = DriveApp.getFileById(getFileIdFromDriveUrl(fileURL));
var content = files.getBlob().getDataAsString();
var json = JSON.parse(content);
//setdata(json);
let docProperties = PropertiesService.getDocumentProperties();
docProperties.setProperty('project_id', json.project_id);
docProperties.setProperty('private_key', json.private_key);
docProperties.setProperty('client_email', json.client_email);
return true
} catch (e) {
var ui = SpreadsheetApp.getUi();
ui.alert(e)
return false
Logger.log(e);
}
}