【问题标题】:Exporting data to google sheet将数据导出到谷歌表
【发布时间】:2018-11-15 13:22:47
【问题描述】:

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

此应用用于控制组织中的 PC 库存。因此,该应用程序有一个模型,该模型由 32 个字段和 1 个关系组成。该模型中已经保存了 2650 条记录。我还有一个将所有记录导出到谷歌表的过程。即使它运行良好,但从我的角度来看,导出消耗了太多时间。

所以我的逻辑包括获取所有记录、遍历每条记录并获取每个字段的数据。然后将所有字段排成一行,最后保存到google sheet;因此它看起来像这样:

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();

此过程大约需要 8-10 分钟才能完成。我相信这可以更快地完成。我知道这是因为如果我转到设置 > 部署 > 导出数据并导出 ALL 数据,只需要 1:30 分钟。很快,考虑到它还导出其他数据。

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

【问题讨论】:

    标签: google-app-maker


    【解决方案1】:

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

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

    var query = app.models.pcItems.newQuery();
    query.prefetch.myModel._add();
    
    var pcItems = query.run();
    
    for (...) {
    
    ...
    
    // after adding prefetch this line should not cause additional
    // call to the database
    hist = item.itemHistory; 
    
    ...
    }
    

    【讨论】:

    • 我能够将导出时间减少到 2 分钟。惊人的!这是一个很棒的建议帕维尔。欣赏!
    • @Morfinismo 您介意发布您的新代码以进行导出吗?我总是想知道如何改进我的脚本以减少不必要的调用并缩短处理时间。
    猜你喜欢
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 2018-09-05
    • 2017-08-12
    • 2023-01-04
    • 1970-01-01
    相关资源
    最近更新 更多