简体   繁体   English

仅备份值或在备份后将公式转换为值

[英]Either backup only values or convert formulas to values after backup

So, I'm trying to backup a google spreadsheet via apps script, because each month the data is going to change, and I want a record of what the data was in the past.所以,我试图通过应用程序脚本备份谷歌电子表格,因为每个月数据都会改变,我想要记录过去的数据。 I can make a copy of the spreadsheet just fine.我可以制作一份电子表格的副本就好了。

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


}

But, the data in the spreadsheet will change based on the formulas within a day or so after I'm done for the month.但是,电子表格中的数据将在我完成一个月后的一天左右内根据公式发生变化。 I only want to copy the values.我只想复制这些值。 I've been scratching my head about this for a few hours now, and was wondering if anyone had any thoughts?几个小时以来,我一直在为此挠头,想知道是否有人有任何想法? I'm not particularly good at coding, so, it'll have to be decently basic responses.我不是特别擅长编码,所以,它必须是体面的基本反应。

One thought I had was to make copies of all the sheets in the spreadsheet, convert those into values, copy those sheets to a new spreadsheet maybe, then delete those copies in the original?我的一个想法是复制电子表格中的所有工作表,将它们转换为值,然后将这些工作表复制到新的电子表格中,然后删除原始工作表中的那些副本? It would be a bit tedious, but there's only 4 tabs right now.这会有点乏味,但现在只有 4 个标签。 "Form" "BankRecords" "Transactions" and "GLAccounts" “表格”“银行记录”“交易”和“GLAccounts”

  • For the copied Spreadsheet, you want to convert the values given by the formulas to only values without the formulas.对于复制的电子表格,您希望将公式给出的值转换为仅没有公式的值。
  • You want to achieve this by modifying your Google Apps Script.您希望通过修改您的 Google Apps 脚本来实现这一点。

Modification point:修改点:

  • In this case, I would like to propose to convert above situation using copyTo to the copied Spreadsheet.在这种情况下,我想建议使用copyTo将上述情况转换为复制的电子表格。

Modified script:修改后的脚本:

When your script is modified as the simple modification, please modify as follows.当您的脚本修改为简单修改时,请进行如下修改。

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

Note:笔记:

  • Please enable V8 at the script editor.请在脚本编辑器中启用 V8。

Reference:参考:

So, I made an ugly workaround.所以,我做了一个丑陋的解决方法。 If anyone wants to tell me how to clean it up, that'd be great, but if not, it's at least functional.如果有人想告诉我如何清理它,那就太好了,但如果没有,它至少是有用的。 My fix: I made an intermediary backup sheet, that was given all permissions to do importranges for each of the sheets required.我的解决方法:我制作了一个中间备份表,它被授予了为每个所需的表执行导入范围的所有权限。 Then I made a function based off this: https://www.labnol.org/code/20239-copy-google-spreadsheets for each tab I wanted backed up.然后我根据这个做了一个函数: https : //www.labnol.org/code/20239-copy-google-spreadsheets为我想要备份的每个标签。 (Lots of duplication, unfortunately, and If I ever want to add a bunch more tabs, it'll be a pain in the neck.) Then after all of those functions, I added back in the original makecopy function, tweaked so that it would copy based on the intermediary backup instead. (不幸的是,有很多重复,如果我想添加更多选项卡,那将是一件令人头疼的事。)然后在所有这些功能之后,我重新添加了原始的 makecopy 功能,进行了调整以使其将根据中间备份进行复制。 Here's the code:这是代码:

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



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM