0
votes

I'm setting up a google-app-maker app. I want to sort a table with a query.

I want to call a script onAttach-Event. Because I need to user the "query" to sort my data, I have to use a server script. In google-app-maker i can't use a query at client script, right?!

But every time I try to call the server script I get an error

SEVERE: Failed due to circular reference

onAttach Event:

google.script.run.withSuccessHandler(function () {

}).sortTable(widget);

The server script:

function sortTable(widget) {

}

I have already a worked server script. This works perfect. I call this from "App startup script" like:

google.script.run.withSuccessHandler(function(reValue) {
  ...
}).initUserData(startCode);

Server script here:

function initUserData(startCode) {
  ...
}

The query looks like:

function sortTable(widget) {
  var people = widget.parent.datasource.relations.People;

  var query = people.newQuery();
  query.sorting.PeopleRole._descending();
  // query.sorting.Name._descending(); // sorted by people.Name
  var records = query.run();
  app.saveRecords([records]);

  ...
}
2
You can't pass a widget to a server script. A widget is an object reference on the client only so when you do people.newQuery() that is where your circular reference comes in because your server script can't interpret the datasource being passed from your widget. If you let us know what you are trying to accomplish maybe we can help. Also, why are you calling a sort table in the app start up vs just from either the client or within the datasource server script? - Markus Malessa
Thanks for the point with the widget. I want to sort a table of people. I want to sort descending by the role of the people. I want every time the same kind of sorted table. So its fine if the table is sorted at the moment when the table page is opened. - Luigi Diablo

2 Answers

1
votes

Based on your comment I would suggest editing your query script for the datasource itself. You can find your datasource settings as depicted in the attached image, you would need to change it to server script vs query builder and then put the following code in there:

query.sorting.peoplerole._descending();
return query.run();

Example

0
votes

I simple user javascript sort() now. I call a client script:

function sortTable(widget) {

  var peopleOfComplany = widget.parent.datasource.relations.PeopleOfComplany.items;
  peopleOfComplany.sort(function(a, b) {
    return b.PeopleRole - a.PeopleRole;
  });

}

it works perfectly. But now i have to figure out how to sort, when PeopleRole is an array.