Google app maker 将数据导出到google工作表

Google app maker 将数据导出到google工作表,google-app-maker,Google App Maker,这更多的是关于性能。这是一个场景: 此应用程序用于控制组织中PC的库存。因此,该应用程序有一个由32个字段和1个关系组成的模型。模型中已保存2650条记录。我还有一个将所有记录导出到google工作表的过程。尽管它工作得很好,但在我看来,导出占用了太多的时间 因此,我的逻辑包括获取所有记录,循环遍历每个记录并获取每个字段的数据。然后将所有字段放入一行,最后将其保存到google工作表中;因此,它看起来是这样的: var allRows =""; header = ["Property Tag",

这更多的是关于性能。这是一个场景:

此应用程序用于控制组织中PC的库存。因此,该应用程序有一个由32个字段和1个关系组成的模型。模型中已保存2650条记录。我还有一个将所有记录导出到google工作表的过程。尽管它工作得很好,但在我看来,导出占用了太多的时间

因此,我的逻辑包括获取所有记录,循环遍历每个记录并获取每个字段的数据。然后将所有字段放入一行,最后将其保存到google工作表中;因此,它看起来是这样的:

var allRows ="";
header = ["Property Tag", "Status", "Building", "Department", "Floor", "Area", "Specific Location", "Serial Number", "Model", "Purchase Date", "Warranty End", "HD Size"];
header.push("Processor", "RAM", "PC Name", "MAC Address", "Monitor 1", "Monitor 1 Model", "Monitor 2", "Monitor 2 Model", "Notes", "Office", "Last Inventoried","SSO Type");
header.push("Static/Reserved IP Address", "Static IP Reason","Card Reader Installed", "Last Repair Issue", "Last Repair Date", "Created By", "Created On");
header.push("Last Modified By", "Last Modified On", "Item Type");    

allRows += header.join() + "\r\n";

//get all pcItems and save them to google sheet
var pcItems = app.models.pcItems.newQuery().run();
for(i=0; i<pcItems.length; i++){

  item = pcItems[i];

  propTag = (item.propertyTag) ? ("'" + item.propertyTag) : "";
  status = item.status || "";
  building = item.building || "";
  dept = item.department || "";
  floor = item.floor || "";
  area = item.area || "";
  specLoc = (item.specificLocation) ? "'" + item.specificLocation : "";
  serialNum = (item.serialNumber) ? "'" + item.serialNumber : "";
  model = item.model || "";
  purchase = (item.purchaseDate) ? Utilities.formatDate(item.purchaseDate, "GMT-6", "MM/dd/yyyy") : "";
  warranty = (item.warrantyEnd) ? Utilities.formatDate(item.warrantyEnd, "GMT-6", "MM/dd/yyyy") : "";
  hd = (item.hdSize) ? "'" + item.hdSize : "";
  processor = item.processor || "";
  ram = item.ram || "";
  pcName = (item.pcName) ? "'" + item.pcName : "";
  macAdd = (item.macAddress) ? "'" + item.macAddress : "";
  monOne = (item.monitor1) ? "'" + item.monitor1 : "";
  monOneMod = item.monitor1Model || "";
  monTwo = (item.monitor2) ? "'" + item.monitor2 : "";
  monTwoMod = item.monitor2Model || "";
  notes = (item.notes) ? "'" + item.notes : "";
  office = item.officeVersion || "";
  lastInv = (item.lastInventoried) ? "'" + item.lastInventoried : "";
  ssoType = item.ssoType || "";
  staticIp = item.staticIpAddress || "";
  staticIpReason = item.staticIpReason || "";
  var cardReader = (item.cardReaderInstalled === true) ? true : (item.cardReaderInstalled === false) ? false : "";
  createdBy = item.createdBy || "";
  createdOn = (item.created) ?  "'" + Utilities.formatDate(item.created, "GMT-6", "MM/dd/yyyy HH:mm") : "";      
  lastRepairDate = (item.lastRepairDate) ? Utilities.formatDate(item.lastRepairDate, "GMT-6", "MM/dd/yyyy") : "";
  lastRepairIssue = item.lastRepairIssue || "";

  //the history relation
  hist = item.itemHistory;    
  if(hist.length){
    lastModifiedBy = hist[hist.length-1].modifiedBy;
    lastModifiedOn = (hist[hist.length-1].modified) ? ("'" + Utilities.formatDate(hist[hist.length-1].modified, "GMT-6", "MM/dd/yyyy HH:mm")) : "";
  } else {
    lastModifiedBy = "";
    lastModifiedOn = "";        
  }

  row = [propTag, status, building, dept, floor, area, specLoc, serialNum, model, purchase, warranty, hd];
  row.push(processor, ram, pcName, macAdd, monOne, monOneMod, monTwo, monTwoMod, notes, office, lastInv, ssoType);
  row.push(staticIp, staticIpReason, cardReader, lastRepairIssue, lastRepairDate, createdBy, createdOn, lastModifiedBy, lastModifiedOn, "PC");

  formattedRow = [];
  for(d=0; d<row.length; d++){        
    cellData = row[d];
    if((typeof(cellData) === "string") && (cellData.indexOf(",") > -1)){
      cellData = '"'+cellData+'"';
    } else if(typeof(cellData) === "object"){
      cellData = Utilities.formatDate(cellData, "GMT", "MM/dd/yyyy");
    }
    formattedRow.push(cellData);
  }

  csvRow = formattedRow.join(); 
  allRows += csvRow+"\r\n";
}

var data = Utilities.newBlob("").setDataFromString(allRows, "UTF-8").setContentType("text/csv");
var newFile = Drive.Files.insert({title: fileName}, data, {convert: true});

var ss = SpreadsheetApp.openById(newFile.id);
var sheet = ss.getActiveSheet();  

var fileHeader = sheet.getRange(1, 1, 1, sheet.getLastColumn());
fileHeader.setBackground("#efefef").setFontWeight("Bold").setVerticalAlignment("Middle");
sheet.setRowHeight(1, 30);
sheet.setFrozenRows(1);

var allData = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
allData.setNumberFormat("@");  
sheet.autoResizeColumns(1, sheet.getLastColumn());  

sheet.deleteColumns(sheet.getLastColumn(), 3);
return ss.getUrl();
var allRows=”“;
页眉=[“物业标签”、“状态”、“建筑”、“部门”、“楼层”、“面积”、“具体位置”、“序列号”、“型号”、“购买日期”、“保修期结束”、“HD尺寸”];
header.push(“处理器”、“RAM”、“PC名称”、“MAC地址”、“监视器1”、“监视器1型号”、“监视器2”、“监视器2型号”、“备注”、“办公室”、“上次清点”、“SSO类型”);
header.push(“静态/保留IP地址”、“静态IP原因”、“已安装读卡器”、“上次维修问题”、“上次维修日期”、“创建人”、“创建日期”);
header.push(“上次修改人”、“上次修改日期”、“项目类型”);
allRows+=header.join()+“\r\n”;
//获取所有pcItems并将其保存到google工作表
var pcItems=app.models.pcItems.newQuery().run();
对于(i=0;i部署>导出数据并导出所有数据,只需1:30分钟。考虑到它还导出其他数据,速度非常快


所以我的问题是…有谁知道更好的方法可以帮助我完成这项任务吗?对于关于这件事的任何意见,我提前非常感谢!

首先我建议您找出代码中的瓶颈。例如,您可以尝试使用
console.time
console.timeEnd
来记录执行时间s、 一旦你知道你的算法中最慢的部分在哪里,你就可以解决如何改进它们

第二件要尝试的事情是使用预回迁。看起来,现在您的脚本调用数据库来访问每个记录的关系。因此,对DB的调用总数是
N*M+1
,其中N是记录总数,M是每个记录的关系数,1是获取无关系记录的初始调用

var query=app.models.pcItems.newQuery();
query.prefetch.myModel._add();
var pcItems=query.run();
对于(…){
...
//添加预回迁后,此行不应导致额外的错误
//调用数据库
hist=item.itemHistory;
...
}

我能够将导出时间缩短到2分钟。太棒了!这是一个很棒的建议Pavel。非常感谢!@Morfinismo您介意为导出发布新代码吗?我一直想知道如何改进我的脚本,以减少不必要的呼叫并缩短处理时间。