Home > Net >  Google apps script: using libraries across different scripts giving me different behavior
Google apps script: using libraries across different scripts giving me different behavior

Time:09-22

I'm using dataframe-js. https://gmousse.gitbooks.io/dataframe-js/content/doc/api/dataframe.html

I have paste the code into a .gs file in a script1.

this code works in script1.

function test(){
  var data =[[1,2,3],['a','b','c']]

  var DataFrame = dfjs.DataFrame
 
  var df = new DataFrame(data)

}

But when add script1 as a library into script2 I can't use the code anymore.

function test(){
 
  var data =[[1,2,3],['a','b','c']]

  var DataFrame = script1.dfjs.DataFrame
 
  var df = new DataFrame(data)

}

I'm getting this error

Error   
ArgumentTypeError: Array while expecting DataFrame | Array | Object.
_construct  @ dataframe-js 1.4.3.gs:3167
Wrapper @ dataframe-js 1.4.3.gs:3191
ArgumentTypeError   @ dataframe-js 1.4.3.gs:4517
_build  @ dataframe-js 1.4.3.gs:6813
DataFrame   @ dataframe-js 1.4.3.gs:6665

I could just paste the code again. But why doesn't this work? Not sure if it is a dataframe-js issue or somehow I'm missing something simple.

CodePudding user response:

When I tested console.log(script1.dfjs.DataFrame) at the client-side, I confirmed that the script works. So, from the error message of ArgumentTypeError: Array while expecting DataFrame | Array | Object., I thought that it might be required to pass the array data by value as the string. I thought that the reason for this issue might be due to this of library side and client-side are different. So when the following modification is reflected in your situation, what result will you obtain?

For library side:

Please copy and paste the following script to the script file of the library side.

function DataFrame(data) {
  return new dfjs.DataFrame(JSON.parse(data));
}

For client-side:

Please test the following script on the client-side.

function test(){
  var data =[[1,2,3],['a','b','c']];
  var df = script1.DataFrame(JSON.stringify(data));

}
  • Related