Chris McMahon
Chris McMahon

Reputation: 23

Trying to condense a repetitive script using iteration i = 0 cannot read properly

I'm trying to convert an already working code into an iterative one. Original:

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



}

New:

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



}

Not entirely sure why it's not working. Any help would be appreciated. This is my first foray into iteration, so, I'm really new.

The error I get is "TypeError: Cannot read property 'getDataRange' of null (line 18, file "Copy of Backup")"

Upvotes: 1

Views: 34

Answers (2)

Chris McMahon
Chris McMahon

Reputation: 23

I figured out a solution:

function makeCopy() {
  var sheetList = ['Main', 'CR', 'GS', 'JD', 'JS', 'JW', 'KS', 'SS', 'TR', 'WJ', 'WK'];
  var sheetList1 = ['Main1', 'CR1', 'GS1', 'JD1', 'JS1', 'JW1', 'KS1', 'SS1', 'TR1', 'WJ1', 'WK1'];

  for (var i = 0; i < sheetList.length; i++) {
    // 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(sheetList1[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);



}

Basically, I was adding a 1 to the end of each item in the list, which screwed everything up, and I put all the vars in the loop.

Upvotes: 0

James D
James D

Reputation: 3152

The variable for the target sheet needs to be inside the loop

   for (var i = 0; i < sheetList.length; i++) {

// target sheet
   var ts = tss.getSheetByName(sheetList[i]);

// Clear the Google Sheet before copy
    ts.clear({contentsOnly: true});

Upvotes: 1

Related Questions