How to make the library work with the caller script PropertiesService? - google-apps-script

Until Google extends the import/export API to container-bound Apps Script projects, I have moved most of my project to a library which can use that API, and then made Google Docs project into a shell that just calls through to the library.
My problem is having the library access the same properties (PropertiesService) as the Google Doc project. Since I have existing users of my Docs Add-on, I need to keep using these properties.
In my Google Doc project, I tried
$.PropertiesService = PropertiesService;
(where $ is my library).
It didn't work. The library kept using its own properties.
So then I tried:
function _mock(obj) {
var ret = {};
for(var key in obj) {
if(typeof obj[key] == 'function') {
ret[key] = obj[key].bind(obj);
} else {
ret[key] = obj[key];
}
}
return ret;
}
$.PropertiesService = _mock(PropertiesService);
Still not working. Trying again:
function _mock(obj) {
var ret = {};
for(var key in obj) {
if(typeof obj[key] == 'function') {
ret[key] = (function(val) {
return function() {
return val.apply(obj, arguments);
};
})(obj[key]);
} else {
ret[key] = obj[key];
}
}
return ret;
}
$.PropertiesService = _mock(PropertiesService);
This works.
At this point, I'm wondering:
Why did the first two ways not work, but the third way did?
Can I expect this to continue working?
Is there a better way to have a library access the main script's properties?
Documentation is sparse. There is this, but the PropertiesService is not mentioned.

Sharing of resources
As you are aware, libraries have shared and non-shared resources. PropertiesService is listed under non-shared resources, meaning that the library has its own instance of the service that is accessed when you reference it in the library code.
const getStore = () => PropertiesService.getScriptProperties();
If the function above is declared in the library, it will use the library's resource, if in the calling script - its own instance.
V8 runtime solution
V8 runtime does not create a special context for your code and gives you access to built-in services directly. Because of this when using the runtime, the service can be injected by simply defining or replacing a property on a global this:
//in the library;
var getProperty = ((ctxt) => (key) => {
var service = ctxt.injectedService;
var store = service.getScriptProperties();
return store.getProperty(key);
})(this);
var setProperty = ((ctxt) => (key, val) => {
var service = ctxt.injectedService;
var store = service.getScriptProperties();
return store.setProperty(key, val);
})(this);
var inject = ((ctxt) => (service) => ctxt.injectedService = service)(this);
var greet = ((ctxt) => () => {
var store = ctxt.injectedService.getScriptProperties();
return store.getProperty("greeting") || "Ola!";
})(this);
//in the calling script;
function testSharedResources() {
PropertiesService.getScriptProperties().setProperty("greeting", "Hello, lib!");
$.inject(PropertiesService);
Logger.log($.greet()); //Hello, lib!
$.setProperty("greeting", "Hello, world!");
Logger.log($.greet()); //Hello, world!
}
In some contexts global this will be undefined (I encountered this when adding a library to a bound script). In this case, simply define a private global namespace (to avoid leaking to the caller script):
//in the library;
var Dependencies_ = {
properties : PropertiesService
};
var use = (service) => {
if ("getScriptProperties" in service) {
Dependencies_.properties = service;
}
};
//in the calling script;
$.use(PropertiesService);
Rhino runtime solution
Older Rhino runtime, on the other hand, creates a special implicit context. This means that you have no access to built-in services or the global this. Your only option is to bypass calling the service in the library (your approach #3 is perfect for doing so).
Questions
Why did the first two ways not work, but the third way did?
All issues with your approaches boil down to:
Resource sharing (libraries have their own service instances)
Special implicit context (no external access to lib built-ins in Rhino)
But there is a catch: all 3 approaches do work as designed.
First, Approach one does work if you specifically reference the PropertiesService on $. This makes sense as the library is included as a namespace with members mapped to global declarations in the library. For example:
//in the caller script
PropertiesService.getScriptProperties().setProperty("test", "test");
$.PropertiesService = PropertiesService;
Logger.log( $.PropertiesService.getScriptProperties().getProperty("test") ); // "test"
Logger.log( $.getProperty("test") ); // "null"
//in the library
function getProperty(key) {
var store = PropertiesService.getScriptProperties();
return store.getProperty(key);
}
Approach two also works. Binding of the function in the caller script does not change the fact if called in the library it receives library context, but if you call the bound copy directly in the calling script, it works:
//in the caller script
PropertiesService.getScriptProperties().setProperty("test", "test");
var bound = $.PropertiesService.getScriptProperties.bind(PropertiesService);
var obj = { getScriptProperties : bound };
$.PropertiesService = obj;
Logger.log( bound().getProperty("test") ); // "test"
Logger.log( $.getProperty("test") ); // "null"
Now, why does the third approach work out of the box? Because of the closure resulting from the wrapped function capturing the PropertiesService of the calling script and applying the getScriptProperties method. To illustrate:
//in the caller script
var appl = {
getScriptProperties : (function(val) {
return function() {
return val.apply(PropertiesService);
};
})(PropertiesService.getScriptProperties)
};
$.PropertiesService = appl;
Logger.log( $.getProperty("test") ); // "test"
Can I expect this to continue working?
Yes and no. Yes, because your _mock function behavior exhibits the expected behavior in all cases. No, because apply relies on the getScriptProperties not being implemented as an arrow function where this override will be ignored.
Is there a better way to have library access the main script's properties?
For Rhino runtime - don't think so. For V8 - direct injection of the service will suffice.

Related

Load template from script level using HtmlService.createHtmlOutputFromFile call via a Google Apps Script library

I have the following function in a library called lib.
var Template =
{
GetContents: function (templateName)
{
var templateContents;
try
{
templateContents = HtmlService.createHtmlOutputFromFile(templateName).getContent();
}
catch (e)
{
return undefined;
}
return templateContents;
},
};
I would like to call this function from a script that uses the library, like so:
templateContents = lib.Template.GetContents('htmltemplate');
However, when I do this it does not return 'htmltemplate' from the script level - it returns 'htmltemplate' from the library level instead. I Would like to retrieve the contents of 'htmltemplate' from the script level, not the library level, how can I get the desired result?

Having issues with Twitter API authorization in Google Apps Script

I've used the documentation posted here https://github.com/gsuitedevs/apps-script-oauth1
I am having issues getting the function to authorize. I am new to working with API's so please bear with me. Trying to do a simple get request from twitter but the first part isn't going through. Any idea where things are going wrong? *Note ive loaded the Oauth1 library.
function getTwitterService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
var service = OAuth1.createService('twitter')
// Set the endpoint URLs.
service.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
service.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
service.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
// Set the consumer key and secret.
service.setConsumerKey('myKey')
service.setConsumerSecret('mySecret')
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties());
function authCallback(request) {
var twitterService = getTwitterService();
var isAuthorized = twitterService.handleCallback(request);
if (isAuthorized) {
return Logger.log('Success! You can close this tab.');
} else {
return Logger.log('Denied. You can close this tab');
}
}
function makeRequest() {
Logger.log(authorizationUrl);
var twitterService = getTwitterService();
var response = twitterService.fetch("https://api.twitter.com/1.1/followers/list.json?screen_name='xyz'");
var post = response.getContentText();
Logger.log(post);
}
}
The getTwitterService() method should return the service object.
Currently, all the other methods in the snippet are declared inside the getTwitterService method.
function getTwitterService() {
return OAuth1.createService('twitter')
.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
.setConsumerKey('myKey')
.setConsumerSecret('mySecret')
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties());
}

Supply API key to avoid Hit Limit error from Maps Service in Apps Script

I have a Google Sheet where we are fetching the driving distance between two Lat/Lng via the Maps Service. The function below works, but the matrix is 4,500 cells, so I'm getting the "Hit Limit" error.
How can I supply my paid account's API key here?
Custom Function
function drivingMeters(origin, destination) {
if (origin=='' || destination==''){return ''}
var directions = Maps.newDirectionFinder()
.setOrigin(origin)
.setDestination(destination)
.getDirections();
return directions.routes[0].legs[0].distance.value ;
}
Example use:
A1: =drivingMeters($E10,G$9)
Where E10 = 42.771328,-91.902281
and G9 = 42.490390,-91.1626620
Per documentation, you should initialize the Maps service with your authentication details prior to calling other methods:
Your client ID and signing key can be obtained from the Google Enterprise Support Portal. Set these values to null to go back to using the default quota allowances.
I recommend storing these values in PropertiesService and using CacheService, to provide fast access. Using this approach, rather than writing them in the body of your script project, means they will not be inadvertently copied by other editors, pushed to a shared code repository, or visible to other developers if your script is published as a library.
Furthermore, I recommend rewriting your custom function to accept array inputs and return the appropriate array output - this will help speed up its execution. Google provides an example of this on the custom function page: https://developers.google.com/apps-script/guides/sheets/functions#optimization
Example with use of props/cache:
function authenticateMaps_() {
// Try to get values from cache:
const cache = CacheService.getScriptCache();
var props = cache.getAll(['mapsClientId', 'mapsSigningKey']);
// If it wasn't there, read it from PropertiesService.
if (!props || !props.mapsClientId || !props.mapsSigningKey) {
const allProps = PropertiesService.getScriptProperties().getProperties();
props = {
'mapsClientId': allProps.mapsClientId,
'mapsSigningKey': allProps.mapsSigningKey
};
// Cache these values for faster access (max 6hrs)
cache.putAll(props, 21600);
}
// Apply these keys to the Maps Service. If they don't exist, this is the
// same as being a default user (i.e. no paid quota).
Maps.setAuthentication(props.mapsClientId, props.mapsSigningKey);
}
function deauthMaps_() {
Maps.setAuthentication(null, null);
}
// Your called custom function. First tries without authentication,
// and then if an error occurs, assumes it was a quota limit error
// and retries. Other errors do exist (like no directions, etc)...
function DRIVINGMETERS(origin, dest) {
if (!origin || !destination)
return;
try {
return drivingMeters_(origin, dest);
} catch (e) {
console.error({
message: "Error when computing directions: " + e.message,
error: e
});
// One of the possible errors is a quota limit, so authenticate and retry:
// (Business code should handle other errors instead of simply assuming this :) )
authenticateMaps_();
var result = drivingMeters_(origin, dest);
deauthMaps_();
return result;
}
}
// Your implementation function.
function drivingMeters_(origin, dest) {
var directions = Maps.newDirectionFinder()
...
}

How to simplify transition from Apps Script libraries to AddOn?

When developing Google Apps Scripts, I make use of libraries since there's lots of shared code. If I have a library A:
function foo() { ... }
Then if I use that library in another script, B, the foo function is exposed via a library resource and gets called like:
function bar() {
A.foo();
...
}
However, the Google AddOn documentation says to never use libraries. So I prep the AddOn by combining all the library files, which puts everything in the same context, and now the same call from bar should be foo() and not A.foo().
I've been trying to think of some trick or way of doing all this that will allow me to combine the files without going through and rewriting all the library function calls (e.g., find 'A.' replace '') or transferring all the functions to an object (e.g., A = { foo: function() {...} }). I'd like to be able to just copy and paste all the library bits, but I can't figure (or find) a way to do it.
Does this little example help? There might be a better/easier way to do it, but it works with minimal editing of the original library. The other common method of exposing a library's functions don't seem to work with apps script.
Option 1:
var a = new A();
function main() {
Logger.log(a.foo());
}
function A() {
var self = this;
self.foo = function() {
return "Hello, world!";
}
function bar() {
return "I'm private!";
}
}
Option 2:
A little more complicated using prototype...
function main() {
var a = new A();
Logger.log(a.foo());
}
var A = function() {
var self = this;
self.bar = function() {
return "World!";
}
}
A.prototype.foo = function() { //Prototype public stuff
return "Hello, " + this.bar();
}
Either way they will have to modified a bit. Does anybody else know a better way?

OAuth in Google Apps Scripts Libraries

I am building some wrapper libraries in GAS for some of the Domain Admin APIs. I have a general library which handles OAuth, UrlFetch, XML to Object and Object to XML functions. I have added this as a Library to my API wrappers. The first one I created was for the EmailAuditAPI. This works just fine when I load the EmailAuditAPI as a library. The second API wrapper I created was for the GmailSettingsAPI. This wrapper works fine within itself, but is not working when I call it from another script where it has been loaded as a library. For the more visual of you
GoogleAPITools -> EmailAuditAPI Wrapper -> Project Script : Works!
GoogleAPITools -> GmailSettingsAPI Wrapper -> Project Script : Doesn't work!
GoogleAPITools -> GmailSettingsAPI Wrapper : Works!
I think this is the important code:
GoogleAPITools
function callGApi(apiUrl,authScope,method,payloadIn)
{
//oAuth & Fetch Arguments
var fetchArgs = googleOAuth_("google", authScope);
fetchArgs.method = method ? method : "GET";
if(payloadIn)
{
fetchArgs.contentType = "application/atom+xml";
fetchArgs.payload = payloadIn;
}
var urlFetch = UrlFetchApp.fetch(apiUrl, fetchArgs); //This line fails w/ not working wrapper
var returnObject = urlFetch.getContentText() != '' ? Xml.parse(urlFetch.getContentText()) : urlFetch.getResponseCode();
return returnObject;
}
//Google oAuth
function googleOAuth_(name,scope)
{
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey("anonymous");
oAuthConfig.setConsumerSecret("anonymous");
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
This wrapper works when loaded into a third script as a library.
function getAccountInfoRequest(user,reqId)
{
var properties = GoogleAPITools.callGApi(ACCOUNTINFOURL + user + "/" + reqId,AUDITSCOPE).entry.property;
return GoolgeAPITools.returnXmlToObject(properties);
}
This wrapper does not work when loaded into a third script as a library, but it does work on its own. The GoogleAPITools is loaded as a library in both wrapper scripts.
GmailSettingsAPI Wrapper
//Get Signature
function getSignatureForUser(user)
{
var returnedInfo = GoogleAPITools.callGApi(EMAILSETTINGSURL + user + "/signature",EMAILSETTINGSSCOPE);
return GoolgeAPITools.returnXmlToObject(new Array(returnedInfo.entry.property));
}
Third project script with both wrappers loaded as libraries:
function testApiWrappers() {
var user = "john";
var reqId = "123456789";
Logger.log(AuditApi.getAccountInfoRequest(user,reqId));
Logger.log(GmailSettingsApi.getSignatureForUser(user)); //Fails here
}
In my log I see the return for the AuditApi call, but I get an "Unexpected error:" that references the URLFetch in the GoogleAPITools script.
Is there something different about the Oauth for the two APIs? Is there something glaring in my code that I missed? Any assistance would be great.