socialpoint-labs / sheetfu-apps-script

A Google apps scripts ORM to manipulate spreadsheets as database tables.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Selector.prototype.evaluate not working as expected when called and Sheetfu is used as library

onlinetocode opened this issue · comments

Hey there,

I encountered a strange error.

When I access Sheetfu as an app script library from the app script editor I can not use an object for the table.select() method. I get an error message "Oops! Criteria should be an Array or an Object. Fix it and try again.". When I try to use an array as input it works fine.

When I try the same code with a clone of the repository created as my own app script files it works as a charm with an object as method input.

I tracked it down to this comparison:

value.constructor === Object

When used from the same App Script project it returns true, but when triggered from an external project where Sheetfu is used as a library it returns false.

I have used the latest version (14) and the master branch from Github. I tried to use the cloned repository as private library as well. That produces the same error as with the offical Sheetfu library.

When I stringify and parse the object in the isObject() like so:

JSON.parse(JSON.stringify(value)).constructor === Object

it works both ways. It seems to me that when objects get passed from one project to another as method parameter something strange happens to them.

But I can't figure out what it is.

When using this function

function tableClassQuickstart() {
    // Let's create a table and search for Philippe.
    var sheetName = 'people';
    var headerRow = 1;
    var table = Sheetfu.getTable(sheetName, headerRow);       
    var item = table.select({"first_name": "Philippe"}).first();
    
    // get values, notes, etc..
    Logger.log(item.getFieldValue("age"));     
}

on your example table:

first_name last_name age
Philippe Oger 36
Guillem Orpinell 25
John Doe 32
Jane Doe 32

I get the following error message:
7:04:55 PM Error
Oops! Criteria should be an Array or an Object. Fix it and try again.
Selector.evaluate @ selector.gs:36
Table.select @ table.gs:245

Best regards,
onlinetocde

Hello! I have had this problem.
See here: #47
My solution: https://github.com/numericmaestro/sheetfu-apps-script

Hi @numericmaestro,

thanks for your solution! I hope Google will fix this problem soon!

Best regards!

This issue seems to be when the library is used in a container-bound script using the V8 runtime
Based on the google documentation here https://developers.google.com/apps-script/guides/v8-runtime/migration#adjust_handling_of_instanceof_in_libraries

I would suggest changing the problematic comparison in the isObject function
from value.constructor === Object
to value.constructor.name === "Object"