备份脚本

时间:2017-09-14 21:14:12

标签: google-apps-script google-sheets

My spreadsheet for containing the archived information

Sample of daily sheet that needs archiving

我有下面的代码,用于插入菜单按钮,以便在一天结束时手动备份,见下文。
当我使用脚本复制此每日工作表时,它会保留所有信息,但我必须授权每个工作表运行脚本。

我的问题是我是否可以在没有任何授权的情况下运行脚本,或者是否有另一种方法可以自动将数据从6张纸中的4张发送到存档表上的相应纸张? 由于这是一次合作,如果我离开或离开,没有人可以授权该剧本 你怎么处理这个?
提前谢谢。

function onOpen() { // This function adds a custom menu to the spreadsheet (Backup to archive) so you can run the script from there.
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Backup')
    .addItem('Backup','dataBackup')
    .addToUi();
}
function nameOfSpreadsheet() {
  var s=SpreadsheetApp.getActive().getName().replace(/(\d{1,2}\.\d{1,2}\.\d{1,2}).*/,'$1');
  return s;
}

function dataBackup() {
  var inputSS = SpreadsheetApp.getActiveSpreadsheet();
  var archiveSS = SpreadsheetApp.openById('146WU8RghfFqlCpCSX7n6kBAKOyxcpVKt14yhVfvYz-g');
  var user = Session.getActiveUser().getEmail();
  var sheetNames = ['AM trip', 'PM trip', 'Pool / Beach', 'Night Dive'];
  for (var i = 0; i < sheetNames.length; i++) {   
    var inputSheet = inputSS.getSheetByName(sheetNames[i]);
    var archiveSheet = archiveSS.getSheetByName(sheetNames[i]);
    var date = inputSheet.getRange('A2').getValue(); // Changed to stop inadvertent cell changes, also made text white so not seen.
    var data = inputSheet.getRange('E7:U37').getValues().filter(function(row) { return row[0] !== '' || row[1] !== ''});

    for (var x = 0; x < data.length; x++) {
      data[x].splice(0, 0, date);
    }

    var getDate = archiveSheet.getRange(archiveSheet.getLastRow(), 1).getValue();
    var maxRowLength = data.reduce(function(length, row) { return Math.max(length, row.length); }, 0);
    var date = new Date(date); 
    var getDate = new Date(getDate);

    if (getDate.getDate() === "Loading Data...") {
      Utilities.sleep(30000);
    }

    if (getDate.getDate() != date.getDate() || getDate.getMonth() != date.getMonth()) {     
      if (data.length != 0) {
        archiveSheet.insertRowsAfter(archiveSheet.getLastRow(), data.length);
         archiveSheet.getRange(archiveSheet.getLastRow() + 1, 1, data.length, maxRowLength).setValues(data);
      } else {
        archiveSheet.insertRowsAfter(archiveSheet.getLastRow(), 1);
        archiveSheet.getRange(archiveSheet.getLastRow() + 1, 1, 1, 2).setValues([[date, 'No Data']]);
      }
    }
  }
}

0 个答案:

没有答案