Search code examples
google-app-maker

Create Table in Google Docs with App Maker


I am trying to create a table in a google docs, from a table in App Maker. So far I have:

function updateDoco() {  
var check = {};  
var projectItemQuery = app.models.ProjectItem.newQuery();
var values = projectItemQuery.run(); 
    values.forEach(function(item){
    if(!check.hasOwnProperty(item.Project)){
      check[item.Project] = true; 
var docId = agResponse.id;                 
var activityGuide = DocumentApp.openById(docId);
var actvityGuideBody = activityGuide.getBody();                  
for (var i = 0; i<values.length; i++) {
var name = values[i].Name;  
var docTitle = actvityGuideBody.insertParagraph(i, name); 
    docTitle.setHeading(DocumentApp.ParagraphHeading.HEADING1); 
}
var steps = app.models.Steps.newQuery();
var activity = steps.run();      
    activity.forEach(function(steps){
     if(!check.hasOwnProperty(steps.Function)){
         check[steps.Functions] = true; 
var table = actvityGuideBody.appendTable();
  for(var n=0; n<activity; n++){
    var step = activity[n].Step;
    var data = [n, step, ''];
    var tr = table.appendTableRow(data);
     }      
   }     
 });

doc.saveAndClose();   

How do I get the results from the query into the table in a google doc?

Thanks for any help you can give!


Solution

  • Ok, here is my best attempt at a solution for you. I would certainly suggest prefetching your ProjectItem steps instead of running a separate query in the for loop. However from there with your code edits it was somewhat lost if you wanted many tables in the same document or otherwise. However here is a potential code solution for you:

    function updateDoco() {
      var check = {};  
      var projectItemQuery = app.models.ProjectItem.newQuery();
      projectItemQuery.prefetch.Function._add(); //prefetch for project item to 
        function relation (many-to-many)
      projectItemQuery.prefetch.Function.Steps._add();  //prefetch for function 
        to steps relation (one-to-many)
      var values = projectItemQuery.run();
    
      for (var i in values) {
        if(!check.hasOwnProperty(values[i].Project)) {
          check[values[i].Project] = true;
        }
        var docId = agResponse.id;                 
        var activityGuide = DocumentApp.openById(docId);
        var activityGuideBody = activityGuide.getBody();
    
        var name = values[i].Name;  
        var docTitle = activityGuideBody.insertParagraph(i, name);
        docTitle.setHeading(DocumentApp.ParagraphHeading.HEADING1);
    
        var functions = values[i].Function;
    
        //loop to run through each function relation on your current ProjectItem
        for (var j in functions) {
          var rowsdata = [['Step','Description']];
          var steps = functions[j].Steps;
          //another loop to run through each step to function relation
          for (var k in steps) {
            rowsdata.push([steps[k].Step,steps[k].Description]);
          }
          activityGuideBody.appendTable(rowsdata);
        }
        doc.saveAndClose();
      }
    }