J. G.
J. G.

Reputation: 1832

Any way to find out the fields in a datasource?

I'd like to write a script to dump the contents of a given table to a spreadsheet regardless of the fields, (I've done this for several individual tables but want to write something universal).

Will

app.models.filesToCopy.fields._values;

be involved?

Upvotes: 0

Views: 148

Answers (1)

Markus Malessa
Markus Malessa

Reputation: 1831

I would say the most generic way would be to establish a server function and pass in the model name as a parameter. Then establish a script similar to what I put below. You can even format your data based on the field type if wanted by using fields[n].type in a if or select statement.

function YourFunction(YourModel) {
  var sheetfile = SpreadsheetApp.create('test file');
  var ss = sheetfile.getActiveSheet();
  var fields = app.metadata.models[YourModel].fields;
  var results = app.models[YourModel].newQuery().run();

  var i, j;
  var sheetdata = [], header = [];

  for (j in fields) {
    header.push(fields[j].displayName);
  }
  sheetdata.push(header);

  for (var i in results) {
    var data = [];
    for (j in fields) {
      data.push(results[i][fields[j].name]);
    }
    sheetdata.push(data);
  }
  ss.getRange(1,1,sheetdata.length,fields.length).setValues(sheetdata);
}

Upvotes: 2

Related Questions