google apps script - Need a way to parametize an array sort function, with parameters set in a handler -
i'm writing web app displays subset of rows spreadsheet worksheet. convenience of users, data presented in grid, each row selected spreadsheet forms row in grid. number of rows relevant each user grows on time.
the header of grid set of buttons, allow user control sort order of data. if user clicks button in header of first column, array populates grid sorted first column , forth. if click same column button twice, sort order reverses.
i used script properties communicate selected sort field , order between handler responding button presses, , order function called sort.
so in doget():
// sort field , order scriptproperties.setproperties({"sortfield": "date","sortorder": "asc"});
and in button handler:
function sorthandler(event) { var id = event.parameter.source; if (id === scriptproperties.getproperty("sortfield")) { scriptproperties.setproperty("sortorder", (scriptproperties.getproperty("sortorder") === "asc")?"desc":"asc"); } else { scriptproperties.setproperties({"sortfield": id, "sortorder": "asc"}); } var app = uiapp.getactiveapplication(); app.remove(app.getelementbyid("scrollpanel")); createform_(app); return app; }
and order function (this invoked sort method on array: array.sort(order);
in code defines grid):
function orderfunction(a, b) { var sortparameter = scriptproperties.getproperties(); var asc = sortparameter.sortorder === "asc"; switch(sortparameter.sortfield) { case "date": var adate = new date(a.date); var bdate = new date(b.date); return (asc)?(adate - bdate):(bdate - adate); case "serviceno": return (asc)?(a.serviceno-b.serviceno):(b.serviceno-a.serviceno); default: // lexical var alex = string(a[sortparameter.sortfield]).tolowercase(); var blex = string(b[sortparameter.sortfield]).tolowercase(); if (alex < blex) { return (asc)?-1:1; } else if (alex > blex) { return (asc)?1:-1; } else { return 0; } } }
the fly in ointment design google. once array gets size, sort fails error properties service being called frequently. message suggests inserting 1s delay using utilities.sleep()
, grid takes long time render - how going go if array takes 1s decide order of 2 values?
i tried reimplementing scriptdb, suffers same problem, calls scriptdb service made google's liking.
so how else can implement this, without orderfunction accessing app script services?
if looking temporary storage, prefer cacheservice
scriptproperties
. use
cacheservice.getprivatecache().put
, cacheservice.getprivatecache().get
Comments
Post a Comment