【问题标题】:Either backup only values or convert formulas to values after backup仅备份值或在备份后将公式转换为值
【发布时间】:2020-04-01 22:15:10
【问题描述】:

所以,我正在尝试通过应用程序脚本备份谷歌电子表格,因为每个月数据都会发生变化,我想要记录过去的数据。 我可以制作电子表格的副本就好了。

function makeCopy() {

// generates the timestamp and stores in variable formattedDate as year-month-date hour-minute-second
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");

// gets the name of the original file and appends the word "copy" followed by the timestamp stored in formattedDate
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + formattedDate;

// gets the destination folder by their ID. REPLACE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx with your folder's ID that you can get by opening the folder in Google Drive and checking the URL in the browser's address bar
var destination = DriveApp.getFolderById("I know this is supposed to be the folderID");

// gets the current Google Sheet file
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())

// makes copy of "file" with "name" at the "destination"
file.makeCopy(name, destination);


}

但是,在我完成一个月后,电子表格中的数据会在一天左右的时间内根据公式发生变化。我只想复制这些值。几个小时以来,我一直在为此挠头,想知道是否有人有任何想法?我不是特别擅长编码,所以,它必须是体面的基本响应。

我的一个想法是复制电子表格中的所有工作表,将它们转换为值,将这些工作表复制到新的电子表格中,然后删除原始电子表格中的这些副本?这会有点乏味,但现在只有 4 个选项卡。 “表格”“银行记录”“交易”和“GLAccounts”

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:
    • 对于复制的电子表格,您希望将公式给出的值转换为仅没有公式的值。
    • 您希望通过修改 Google Apps 脚本来实现此目的。

    修改点:

    • 在这种情况下,我建议将上述使用copyTo 的情况转换为复制的电子表格。

    修改脚本:

    当您的脚本被修改为简单修改时,请进行如下修改。

    从:
    file.makeCopy(name, destination);
    
    到:
    const copiedSpreadsheet = file.makeCopy(name, destination);
    SpreadsheetApp.open(copiedSpreadsheet).getSheets().forEach(r => {
      const range = r.getDataRange();
      range.copyTo(range, {contentsOnly: true});
    });
    

    注意:

    • 请在脚本编辑器中启用 V8。

    参考:

    【讨论】:

    • 它很接近,它似乎只使用值进行复制,但是我有一些导入范围显示为 #REF 而不是它显示的值:\
    • @Chris McMahon 感谢您的回复。我带来的不便表示歉意。为了正确了解您的情况,您能否提供一个示例电子表格来复制您的问题?通过这个,我想考虑一下解决方案。
    【解决方案2】:

    所以,我做了一个丑陋的解决方法。如果有人想告诉我如何清理它,那就太好了,但如果没有,它至少是功能性的。我的修复: 我制作了一张中间备份表,它被授予了为所需的每张表执行导入范围的所有权限。 然后我根据这个做了一个函数:https://www.labnol.org/code/20239-copy-google-spreadsheets 对于我想要备份的每个选项卡。 (不幸的是,有很多重复,而且如果我想添加更多标签,那将是一件令人头疼的事情。) 然后在完成所有这些功能之后,我重新添加了原始的 makecopy 功能,并进行了调整,以便它可以根据中间备份进行复制。 代码如下:

    function onOpen() {
      var ui = SpreadsheetApp.getUi();
      // Or DocumentApp or FormApp.
      ui.createMenu('Custom Scripts')
          .addItem('Backup', 'cloneGoogleSheet')
          .addToUi();
    }
    
    function cloneGoogleSheet() {
    cloneMain()
    cloneCR()
    cloneGS()
    cloneJD()
    cloneJS()
    cloneJW()
    cloneKS()
    cloneSS()
    cloneTR()
    cloneWJ()
    cloneWK()
    makeCopy()
    }
    
    function cloneMain(Main, Main1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('Main');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('Main1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneCR(CR, CR1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('CR');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('CR1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneGS(GS, GS1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('GS');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('GS1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneJD(JD, JD1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('JD');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('JD1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneJS(JS, JS1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('JS');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('JS1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneJW(JW, JW1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('JW');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('JW1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneKS(KS, KS1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('KS');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('KS1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneSS(SS, SS1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('SS');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('SS1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneTR(TR, TR1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('TR');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('TR1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneWJ(WJ, WJ1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('WJ');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('WJ1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function cloneWK(WK, WK1) {
    
      // source doc
      var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
    
      // source sheet
      var ss = sss.getSheetByName('WK');
    
      // Get full range of data
      var SRange = ss.getDataRange();
    
      // get A1 notation identifying the range
      var A1Range = SRange.getA1Notation();
    
      // get the data values in range
      var SData = SRange.getValues();
    
      // target spreadsheet
      var tss = SpreadsheetApp.openById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE');
    
      // target sheet
      var ts = tss.getSheetByName('WK1');
    
      // Clear the Google Sheet before copy
      ts.clear({contentsOnly: true});
    
      // set the target range to the values of the source data
      ts.getRange(A1Range).setValues(SData);
    
    };
    
    function makeCopy() {
    
    // generates the timestamp and stores in variable formattedDate as year-month-date hour-minute-second
    var formattedDate = Utilities.formatDate(new Date(), "EST5EDT", "yyyyMMdd' 'HH:mm");
    
    // gets the name of the original file and appends the word "copy" followed by the timestamp stored in formattedDate
    var name = "Master Sheet - " + formattedDate;
    
    // gets the destination folder by their ID. REPLACE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx with your folder's ID that you can get by opening the folder in Google Drive and checking the URL in the browser's address bar
    var destination = DriveApp.getFolderById("1E5EodsDaaT6a8wkRZ-NtmszzV9DTgQok");
    
    // gets the intermediary backup
    var file = DriveApp.getFileById('1NP7GNjnGHpkRcm7nG_-_Of8f--zQiG7GxO_aOY3ppXE')
    
    // makes copy of "file" with "name" at the "destination"
    file.makeCopy(name, destination);
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2022-10-19
      • 1970-01-01
      • 2021-07-13
      • 2016-04-13
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 2014-05-31
      • 2017-01-16
      相关资源
      最近更新 更多