0

有没有办法对 App Maker 数据库进行夜间备份?以防万一用户不小心删除了任何数据?

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

4

1 回答 1

2

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

这段代码将在服务器端脚本上,看起来像这样:

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

于 2017-03-17T13:10:45.127 回答