简化存档脚本

时间:2017-09-02 21:10:34

标签: google-apps-script

我在下面输入了一个脚本,我试图在具有多个页面的日常工作表中使用该脚本,我有一个存档表,其中的信息将在业务结束时复制。两组工作表都相同,以便于备份。但是我输入的日期有很多空行而不是其他信息。我有一个菜单UI,为员工添加一个按钮,以便在一天结束时进行备份,再次使生活更轻松。我还有一些附加到该文件的其他脚本,因此不确定(很可能是“字体和对齐”脚本导致问题。 谁能看到我做错了什么?

   function menu() { // 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 to archive')
.addItem('Backup', 'dataBackup')
.addToUi();
}
function dataBackup() {

var check = alert("Data Backup","Are you sure you want to backup today's entries?");
if(!check){ return; }
var inputSS = SpreadsheetApp.getActiveSpreadsheet();
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 = SpreadsheetApp.openById('146WU8RghfFqlCpCSX7n6kBAKOyxcpVKt14yhVfvYz-g');

    var date = inputSheet.getRange('A1').getValue(); // Gets todays date from cell A1
    var dataLen = inputSheet.getRange('E7:A37').getValues().filter(String); // Gets the number of entries made today
    var dataRange = inputSheet.getRange('E7:U37'); // Gets the range of cells A7:U37
    var data = dataRange.getValues().slice(0, dataLen.length); // Removes any rows that don't have a number in the '#' column

    for (var x = 0; x < data.length; x++) { // Adds todays date to the start of each row.
        var temp = data[x].splice(0, 0, date)
    }
    var getDate = archiveSheet.getRange(archiveSheet.getLastRow(), 1).getValue();        

    if (getDate != date) { // Checks for duplicate backup          

        if (data.length != 0) { // If there are entries with todays date
            archiveSheet.insertRowsAfter(archiveSheet.getLastRow(), data.length); // Inserts the required amount of rows
            archiveSheet.getRange(archiveSheet.getLastRow() + 1, 1, data.length, data[0].length).setValues(data).setNumberFormat("@"); // inserts the data to the archive sheet
        } else {
            archiveSheet.insertRowsAfter(archiveSheet.getLastRow(), 1); // If there was no data, inserts 1 row
            archiveSheet.getRange(archiveSheet.getLastRow() + 1, 1, 1, 2).setValues([[date, 'No Data']]); // Inserts todays date and 'No Data'
        }
    }

}


}
    function alert(title, message)
      var ui = SpreadsheetApp.getUi();
     var alert = ui.alert(title, message, ui.ButtonSet.YES_NO);
     var response;
  if(alert == "YES"){
  response = true
  } else {
  response = false
}
  return response;
}

以下链接适用于存档表: - https://docs.google.com/spreadsheets/d/146WU8RghfFqlCpCSX7n6kBAKOyxcpVKt14yhVfvYz-g/edit?usp=sharing 以下是输入表的示例: - https://docs.google.com/spreadsheets/d/1XwPFgVP_DlsBp4Th8pR7qKvx4Bh1zUubHabgMdDD3ck/edit?usp=sharing 谢谢。

 Below is the now working backup script.  I have an odd issue though that the menu button isn't appearing until I call it manually from the script editor?  Each time I am asked to review permissions but never had that issue before, is it because of another running script?  Maybe the font alignment one?

 }

  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('A1').getValue();
    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() != 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']]);
        }
    }

}

@SpiderPig好的,它正在运行,现在我不确定下面的脚本是什么问题。我已将工作表名称单元格调整为A2,以避免意外删除,因为A1是打开时的活动单元格,并且有实例在其中键入了内容。但现在在档案表中我得到的日期是01/01/1970,尽管A2有01.01.18。没有数据是正确的。我已尝试将列更改为日期格式,并确保输入表格为日期格式。我还必须每次都对脚本进行授权,但如果我能帮助它,则不想这样做。我是否可以在凌晨2点的时间触发器上运行脚本,这仍然需要为我制作的每个副本进行授权。想要使用正确的日期/日期名称制作一年中每一天的副本。

     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 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() != 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']]);
        }
    }

}    }

1 个答案:

答案 0 :(得分:1)

我试着稍微改进一下脚本。我也改变了比较日期的方式,因为我觉得旧的方式可能无法可靠地运作。

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('A1').getValue();
        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);

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

    }
}