自动从Google AppMaker Datasource导出数据

时间:2018-03-01 03:39:59

标签: javascript google-app-maker

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

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

真的很感激,如果有人知道如何解决这个问题:)

2 个答案:

答案 0 :(得分:1)

这可以通过使用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";

}

我希望这有帮助!

答案 1 :(得分:0)

您似乎正在寻找每日数据库备份。如果到目前为止尚未完成此操作,App Maker Team recommends会将应用迁移到Cloud SQL。开始使用Cloud SQL作为数据后端后,您可以通过Google Cloud Console配置备份: https://cloud.google.com/sql/docs/mysql/backup-recovery/backups