每晚备份App Maker数据

时间:2017-03-11 02:21:03

标签: google-app-maker

有没有办法对App Maker数据库进行夜间备份?以防用户意外删除任何数据?

即使只是输出电子表格也是可以接受的。

1 个答案:

答案 0 :(得分:2)

您可以创建一个基于Installable Trigger的时钟,该时钟将在办公时间之前的早上每天执行一次。

这段代码将位于服务器端脚本上,如下所示:

function createInstallableTrigger() {
  // Runs at 5am in the timezone of the script
  ScriptApp.newTrigger("backUp")
  .timeBased()
  .atHour(5)
  .everyDays(1) // Frequency is required if you are using atHour() or nearMinute()
  .create();
}

function backUp() {
  try {
    var spreadSheet = SpreadsheetApp.openById("").getActiveSheet(),
        dataToBackUp = [],
        globalKeys = {
          model: ["first_name", "last_name", "email"],
          label: ["First Name", "Last Name", "Email"]
        },
    var records = app.models.requests.newQuery().run();
    if(records.length >= 1) {
      for (var i = 0; i < records.length; i++) {
        var newLine = [];
        for (var x = 0; x < globalKeys.model.length; x++) {
          newLine.push(records[i][globalKeys.model[x]]);
        }
        dataToBackUp.push(newLine);
        // at the end, push it all on the spreadsheet
        if(i === records.length - 1) {
          // check if there is any entry at all
          if(dataToBackUp.length >= 1) {
            // append column titles first
            spreadSheet.appendRow(globalKeys.label);
            // 
            spreadSheet.getRange(2, 1, dataToBackUp.length, globalKeys.model.length).setValues(dataToBackUp);
          }
        }
      }
    }
  } catch(e) {
    console.log(e);
  }
}