我正在尝试通过App Maker中的表格在google文档中创建表格。到目前为止,我有:
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();
如何从查询中获取结果到Google文档的表中?
感谢您提供的任何帮助!
答案 0 :(得分:1)
好的,这是我为您提供解决方案的最佳尝试。我当然建议您预取您的ProjectItem步骤,而不要在for循环中运行单独的查询。但是,如果您想在同一文档中创建多个表,则从那里进行代码编辑会有些丢失。但是,这是为您提供潜在的代码解决方案:
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();
}
}