Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsproj4js

Google app scripts proj4js library integration


Good people of stackoverflow.

I would like to write a custom function in google sheets for coordinate transformation. The function shuld transform coordinates in Slovene natioanl grid (EPSG: 3912) to WGS84 (EPSG: 4326). I think that this could be elegantly done with proj4js library.

But since I am complety new to javascript and Google app scripts I am wondering if it is possible to import a library to app scripts. I found out that proj4js is hosted on cdnjs and could be directly used in browser applications but I dont know how to import it in Google app scripts.

I tried to add this library by Script ID, where I used the proj4js cdnjs links as Script ID but I got an error: "Unable to look up library. Check the ID and access permissions and try again."

I would be really gratefull if anybody could help me overcome this rookie obstacle.


Solution

  • Fetch the content from the library URL with UrlFetchApp.fetch, and evaluate the retrieved content with eval().

    You can then use this library.

    Code sample:

    const libraryUrl = "https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.6.3/proj4.min.js";
    eval(UrlFetchApp.fetch(libraryUrl).getContentText());
    proj4(...);