Thanks to the help of this community I successfully completed a crazy idea: To write some ocaml functions to use inside Google Apps Script for a small stupid spreadsheet that I had.
The way it works now is by having a main index.js file that calls the Ocaml functions that are available under a global Lib namespace. Everything is bundled using parcel and the Idea was to use as few JS code as possible. Because it was easier than I expected I decided to go one step further and write some bindings for the GAS functions I was using and reduce the glue JS code even more.
This are the bindings that I wrote so far. They work, but are not usable inside Ocaml yet.
type spreadsheet type sheet type range external getActiveSpreadsheet : unit -> spreadsheet = "getActiveSpreadsheet" [@@bs.val][@@bs.scope "SpreadsheetApp"] external getSheets : spreadsheet -> sheet array = "getSheets" [@@bs.send] external getSheetByName : spreadsheet -> string -> sheet = "getSheetByName" [@@bs.send] external getDataRange : sheet -> range = "getDataRange" [@@bs.send] external getValues : range -> 'a array array = "getValues" [@@bs.send]
My doubt are on the edges. When it is just obscure GAS stuff I have no doubt, abstract types and functions to interact with them. Is when a GAS function returns data where I have doubts. Usually they are just arrays of arrays of Numbers or Strings. In the example above, the last definition says that you will get an array of arrays of
'a, but that is not true because it will be an array of “stuff” (strings, numbers, floats).
How should I type it in a way that it’s flexible but not cumbersome? For example, I don’t think using a functor will help because you will need to create a functor for every possible return type, in my case if you have 3 sheets with 3 different shapes, you will need 3 functors.
An alternative that I have used was to provide some helper functions to convert from JS to Ocaml types and then unwrap the Ocaml types, like the example I’m doing with Number_or_string.
This is nothing serious and I will just add the bindings that I may need for now, but I want to hear what the community (and potential users) thinks.
If anyone is interested in taking a look on the project, it is here: https://github.com/danielo515/ElectronicProjectsSpreadsheet