0

I used a function to download some selections from a Lists object as a CSV. I'd like to do the same for the entire datasource connected to the Lists object, but am unsure of how to scale this to work with, as an example, 100,000 rows. On the button to download the List data as a CSV I have:

var rows = widget.root.descendants.MainTableBody.children._values;
var csvdata = [];

csvdata.push([["Email"],["Last Login"],["Sku ID"],["Sku Name"],["Docs Added Recently"]]);

for (var i in rows) {
  var t = [];
  t.push(rows[i].children.EmailField.text);
  t.push(rows[i].children.LastLoginField.text);
  t.push(rows[i].children.SkuIdField.text);
  t.push(rows[i].children.SkuNameField.text);
  t.push(rows[i].children.DocsAddedField.text);
  csvdata.push(t);
}

console.log(csvdata);
exportToCsv("LMexport",csvdata);

The export function is taken from this answer.I basically need the rows var to cover the entire table, but that's a lot of data.

The schema of the datasource in question:

schema

and the calculation used: schema calculation

Here's what the table looks like in the UI for reference:

UI screenshot

Ian Hyzy
  • 481
  • 5
  • 26
  • There's no `docsAdded` parameter here. ``Select`` selects 5 parameters, but none of them is docsAdded. – TheMaster Apr 18 '19 at 16:18
  • DocsAdded is in `ActivityReport`, which gets joined in - I can see the data in the UI - I'll include a screenshot – Ian Hyzy Apr 18 '19 at 20:09
  • To be joined in, it should be selected somewhere. https://www.sqltutorial.org/sql-inner-join/ All I see is Email, skuId, SkuName from m (Licensees) and Email and lastLogin from l(Activity Report). Either I'm missing something or DocsAdded is added after sql query. – TheMaster Apr 18 '19 at 21:37
  • It's not added in anywhere else - I'm not sure what you mean, sorry. After doing that it pulled in the DocsAdded var from the ActivityReport table and I was able to ue it. – Ian Hyzy Apr 22 '19 at 20:50

1 Answers1

0
  • Records retrieved from query are array of objects
  • You can use .reduce to convert them to csv

Snippet:

function exportCsv(){
 var query = app.models.Employees.newQuery();
 //query.filters.... use query used to populate the datasource
 var records = query.run(); //[{},{},{}...]
 var csv = records.reduce(function(str, rec){
  var email = rec.Email;
  var login = rec.LastLogin;
  var skuId = rec.SkuId;
  return str + '"' + [email,login,skuId].join('","') + '"\n'; // result: "email","login","skuId"\n
},'');
return csv;
}
Community
  • 1
  • 1
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • The newQuery command doesn't seem to be available on the calculated datasource - I can use `query` but I'm not sure how to format it with no parameters to just get everything. – Ian Hyzy Apr 15 '19 at 21:44
  • @Ian This is a server script. I don't think it's wise to attempt 100,000 rows client side. You can however call the server script from client side using `google.script.run()` – TheMaster Apr 15 '19 at 21:59
  • sorry about that, my bad. I put it in the server scripts area but I get the error `Cannot find field DocsAdded in the table schema!`. The datasource is a calculated datasource and that field is in there as a number. – Ian Hyzy Apr 16 '19 at 15:58
  • @Ian I guess you need to calculate the field again from the model through apps script. – TheMaster Apr 16 '19 at 16:02
  • I'm not seeing an option to recalculate the model? – Ian Hyzy Apr 17 '19 at 21:36
  • @Ian I have no idea how DocsAdded is calculated. You need to replicate the logic(DocsAdded calculation logic) using apps script. Can you edit your question with screenshots of calculated datasource and the current script you're using? More details and showing your effort increases your chances of getting a solution – TheMaster Apr 17 '19 at 22:35
  • Sorry, let me clarify - the calculation is simple, it's just pulling the data from other source databases. I'll add in more info of the datasources. – Ian Hyzy Apr 18 '19 at 16:09