Я пытаюсь преобразовать уже работающий код в итеративный. Оригинал:
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);
}
Новый:
function makeCopy() {
var sheetList = ['Main', 'CR', 'GS', 'JD', 'JS', 'JW', 'KS', 'SS', 'TR', 'WJ', 'WK'];
// source doc
var sss = SpreadsheetApp.openById('1VM1Pf4PQyP2V_oNDcTHADdqQ-9hY8vALHuEnuIhOKG4');
// source sheet
var ss = sss.getSheetByName(sheetList[i]);
// 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(sheetList[i])+"1";
for (var i = 0; i < sheetList.length; i++) {
// 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);
}
// 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);
}
Не совсем уверен, почему он не работает. Любая помощь будет оценена. Это мой первый набег на итерацию, поэтому я действительно новичок.
Я получаю ошибку «TypeError: Невозможно прочитать свойство 'getDataRange' из null (строка 18, файл" Copy of Backup ")"