有谁知道我们如何从Google AppMaker的数据源中的数据自动生成(例如,在12a.m.生成报告)而不是在用户每次需要报告时手动单击部署中的导出数据生成报告

我在Exporting data out of Google AppMaker上看到了类似的内容,但也没有人试图回答。

非常感谢是否有人知道如何解决这个问题:)

最佳答案

这可以通过使用Installable Triggers来实现。

举例来说,您有一个包含学生数据的模型,该模型包含三个字段。名称(字符串),年龄(数字)和年级(数字)。在服务器脚本上,您可以编写如下内容:

//define function to do the data export
function dataExport() {

  //create sheet to populate data
  var fileName = "Students List " + new Date(); //define file name
  var newExport = SpreadsheetApp.create(fileName); // create new spreadsheet
  var header = ["Name", "Age", "Grade"]; //define header
  newExport.appendRow(header); // append header to spreadsheet

  //get all students records
  var ds = app.models.students.newQuery();
  var allStudents = ds.run();

  for(var i=0; i< allStudents.length; i++) {

    //get each student data
    var student = allStudents[i];
    var studentName = student.name;
    var studentAge = student.age;
    var studentGrade = student.grade;

    var newRow = [studentName, studentAge, studentGrade]; //save studen data in a row
    newExport.appendRow(newRow); //append student data row to spreadsheet

  }

  console.log("Finished Exporting Student Data");

}

//invoke function to set up the auto export
function exportData(){

  //check if there is an existing trigger for this process
  var existingTrigger = PropertiesService.getScriptProperties().getProperty("autoExportTrigger");

  //if the trigger already exists, inform user about it
  if(existingTrigger) {

    return "Auto export is already set";

  } else { // if the trigger does not exists, continue to set the trigger to auto export data

    //runs the script every day at 1am on the time zone specified
    var newTrigger = ScriptApp.newTrigger('dataExport')
    .timeBased()
    .atHour(1)
    .everyDays(1)
    .inTimezone("America/Chicago")
    .create();

    var triggerId = newTrigger.getUniqueId();

    if(triggerId) {
      PropertiesService.getScriptProperties().setProperty("autoExportTrigger", triggerId);
      return "Auto export has been set successfully!";
    } else {
      return "Failed to set auto export. Try again please";
    }

  }

}

然后,要删除/停止自动导出,如果需要,也可以在服务器脚本上编写以下内容:
function deleteTrigger() {

  //get the current auto export trigger id
  var triggerId = PropertiesService.getScriptProperties().getProperty("autoExportTrigger");

  //get all triggers
  var allTriggers = ScriptApp.getProjectTriggers();

  //loop over all triggers.
  for (var i = 0; i < allTriggers.length; i++) {

    // If the current trigger is the correct one, delete it.
    if (allTriggers[i].getUniqueId() === triggerId) {

      ScriptApp.deleteTrigger(allTriggers[i]);
      break;

      //else delete all the triggers found
    } else {

      ScriptApp.deleteTrigger(allTriggers[i]);

    }

  }

  PropertiesService.getScriptProperties().deleteProperty("autoExportTrigger");
  return "Auto export has been cancelled";

}
  • 您可以检查演示应用程序right here
  • 对脚本属性服务is here的引用。
  • 对时区列表is here的引用。

  • 我希望这有帮助!

    关于javascript - 自动从Google AppMaker数据源导出数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49042255/

    10-13 07:37
    查看更多