Script to copy workbook to workbook data only - google-apps-script

Script would run in the source workbook.
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1yeS6_qhURUYHVkImLNMjdGUeMWhPDwQKdLNgpygiwG4'); // sss = source spreadsheet
var ss = sss.getSheetByName('Sheet1'); // ss = source sheet
//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();
var tss = SpreadsheetApp.openById('1KQBsMJ0vgBQwkM89d166RPyVahIZY3DRSOWVCB4zapE'); // tss = target spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName('Sheet1');
sheet.clear({ formatOnly: false, contentsOnly: true });
var tss = SpreadsheetApp.openById('1KQBsMJ0vgBQwkM89d166RPyVahIZY3DRSOWVCB4zapE');
var ts = tss.getSheetByName('Sheet1'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
Would set to trigger on form submit. Would copy the data from a sheet in one workbook to a sheet in another workbook. Format of both sheets will be same AND must remain unaltered. Row & column count/size will be same on each sheet.

Here a code that will go through all the sheet of your source workbook and copy data in the current workbook. Sheet name will be the same of source spreadsheet and only data are copied.
EDIT : Code changed to not copy but import data from spreadsheet to a dest spreadsheet where we assume that the sheet is already created. If sheet does not exist we create it.
function importWorkBook(){
var idSource = "IdOfTheSheetSource";
var sheets = SpreadsheetApp.openById(idSource).getSheets();
var current = SpreadsheetApp.getActiveSpreadsheet();
for(var i in sheets){
var sheet = sheets[i];
var data = sheet.getDataRange().getValues();
var destSheet = current.getSheetByName(sheet.getName());
if(destSheet){
destSheet.getRange(1,1,destSheet.getMaxRows(),destSheet.getMaxColumns()).clearContent();
destSheet.getRange(1,1,data.length,data[0].length).setValues(data);
Logger.log(sheet.getName() + " imported");
}else{
var newSheet = current.insertSheet(sheet.getName());
newSheet.getRange(1,1,data.length,data[0].length).setValues(data);
Logger.log(sheet.getName() + " created");
}
}
}
Stéphane

Related

Referencing Cell for sheet name TypeError: Cannot read property 'copyTo' of null Google Scripts

I am working on a function where I copy a template sheet to another sheet.
Right now I have a demo where I hardcode the name of the template sheet that I want to add.
function AddFeature(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Defines Sheet that uses the function
var sheet = ss.getSheetByName("Client Balance");
//Get the template sheet
var templateSheet = ss.getSheetByName("Summary Sheet");
var totalrow = sheet.getLastRow();
for (var r = 2; r <= totalrow; r++){
var url = sheet.getRange(r,2).getValue();
var destSheet = SpreadsheetApp.openByUrl(url);
//Paste the template sheet to destination spreadsheet
templateSheet.copyTo(destSheet);
destSheet.getSheetByName("Copy of Summary Sheet").activate();
destSheet.moveActiveSheet(1); //Move the pasted template sheet to sheet at Index
destSheet.getSheetByName("Copy of Summary Sheet").setName("Summary Sheet"); //Rename the sheet
}
}
I want my script to draw the name of the sheet it needs to copy from a cell. I tried using this:
var fname = sheet.getRange(1,3).getValue;
var cname = sheet.getRange(1,4).getValue;
Where I would replace all "Summary Sheet" with : fname
and all "Copy of" with: cname
and they come from my sheet that looks like this:
Minimal reproductive example:
function AddFeature(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Defines Sheet that uses the function
var sheet = ss.getSheetByName("Client Balance");
//Defines which cells have the sheet name and "Copy of" sheet name
var fname = sheet.getRange(1,3).getValue;
var cname = sheet.getRange(1,4).getValue;
//Get the template sheet
var templateSheet = ss.getSheetByName(cname);
var totalrow = sheet.getLastRow();
for (var r = 2; r <= totalrow; r++){
var url = sheet.getRange(r,2).getValue();
var destSheet = SpreadsheetApp.openByUrl(url);
//Paste the template sheet to destination spreadsheet
templateSheet.copyTo(destSheet);
destSheet.getSheetByName(cname).activate();
destSheet.moveActiveSheet(1); //Move the pasted template sheet to sheet at Index
destSheet.getSheetByName(cname).setName(fname); //Rename the sheet
}
}
The statements that get the sheet names try to fetch the values from cells C1 and D1. Your screenshot suggests that the values should instead be fetched from cells C2 and D2. Try this:
var fname = sheet.getRange('C2').getValue();
var cname = sheet.getRange('D2').getValue();

Google Sheet (Script) copy Sheet to another SpreadSheet and name new sheet in target SS

I am not that good in script cause i begin, so i want to copy a sheet from a SpreadSheet (source) and copy it to my target SpreadSheet by creating a new sheet (in target SS) and name it with my value in cell C9 in my source sheet....
Actually the new sheet is create with the new name but nothing Copy in the target sheet. It seem to be my .copyTo(targetrange) who as the problem... I try a lot of think but nothing works.
can you help me!
function CopyToSpreadSheet() {
//Source sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Reception");
//Name new sheet in target SpreadSheet from value in cell C9
var nameSheet = sourceSheet.getRange(9, 3).getValue();
//open target SpreadSheet
var target = SpreadsheetApp.openById('SpreadSheet_Id');
var newSheet = target.insertSheet();
var Sheetname = newSheet.setName(nameSheet).getSheetName();
//CopyTo...
var targetSheet = target.getSheetByName(nameSheet);
var targetrange = targetSheet.getRange(1, 1, targetSheet.getLastRow()+1, targetSheet.getLastColumn()+1);
var sourceRange = sourceSheet.getRange(1, 1, 80, 15).copyTo(targetrange).getValues();
targetrange.setValues(sourceRange);
return;
}```
Solution
The copyTo method is available for Sheet Objects and for Range Objects. If you want to use it on a Range instance as you are doing at:
var sourceRange = sourceSheet.getRange(1, 1, 80, 15).copyTo(targetrange).getValues();
It has a limitation: you cannot copy a range to a different Spreadsheet.
Proposed modification
You can use the copyTo method of a Sheet instance in order to achieve the desired result:
function CopyToSpreadSheet() {
//Source sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Reception");
//Name new sheet in target SpreadSheet from value in cell C9
var nameSheet = sourceSheet.getRange(9, 3).getValue();
//open target SpreadSheet
var target = SpreadsheetApp.openById('target-sheet-id');
//CopyTo...
var targetSheet = sourceSheet.copyTo(target);
targetSheet.setName(nameSheet);
return;
}
Reference
Sheet copyTo

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")"
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});
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.

Copy data from a SpreadSheet to another based on filtered range

I have this script that copy the entire content from source spreadsheet to the target spreadsheet.
But I would to copy only a specific column (Col B) dependent form another column (Col C) with the content "Italy".
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1jtZli...'); // sss = source spreadsheet
var ss = sss.getSheetByName('Sorgente'); // ss = source sheet
//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();
var tss = SpreadsheetApp.openById('185I...'); // tss = target spreadsheet
var ts = tss.getSheetByName('Destinazione'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
I have reached this conclusion. There is a method in the destination sheet (where there is the data validation from the source sheet) where the data validation display only the value not inserted yet in the destination sheet?
Try this:
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1jtZli...');
var ss = sss.getSheetByName('Sorgente');
var SRange = ss.getDataRange();
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var B=[];
SData.forEach(function(r){
if(r[2]=='Italy') {
B.push([r[1]]);
}
});
var tss = SpreadsheetApp.openById('185I...');
var ts = tss.getSheetByName('Destinazione');
ts.getRange(1,1,B.length,1).setValues(B);
}
Here's a couple of ways that you can play with. I think the first one will be much faster.
The first one get's data all at one and writes output data all at one time.
function Copy() {
var ss=SpreadsheetApp.openById('source spreadsheet id')
var ssh=ss.getSheetByName('sorg');
var dss=SpreadsheetApp.openById('destination spreadsheet id')
var dsh=dss.getSheetByName('dest')
var srg=ssh.getRange(1,1,ssh.getLastRow(),ssh.getLastColumn());
var svA=srg.getValues();
var oA=[];
svA.forEach(function (r,i) {
if (r[2]=='Italy') {
oA.push([r[1]]);
}
});
dsh.getRange(dsh.getLastRow()+1,1,oA.length,1).setValues(oA);
}
The second one get column B and the column C and compares each row of column C with the string 'Italy' if column C equals "Italy" then in appends that same row of column B so it does a lot more writes and one more read.
function Copy() {
var ss=SpreadsheetApp.openById('source spreadsheet id');
var ssh=ss.getSheetByName('sorg');
var dss=SpreadsheetApp.openById('destination spreadsheet id');
var dsh=dss.getSheetByName('dest');
var srgB=ssh.getRange(1,2,ssh.getLastRow());//col B
var srgC=ssh.getRange(1,3,ssh.getLastRow());//col C
var BvA=srgB.getValues();
var CvA=srgC.getValues();
var oA=[];
BvA.forEach(function (r,i) {
if(CvA[i][0]=='Italy') {
dss.appendRow([BvA[i][0]]);
}
});
}

Multiple sheet copying from a working sheet to an archive sheet

I have had some help with the last line of this code and it works perfectly for a single sheet in the spreadsheet, however I have 5 sheets in the From spreadsheet and identical 5 sheets in the target spreadsheet how can I get the same range in the other 4 sheets to their same named counterpart in the target sheet? So I used below to try and copy each sheet to its corresponding namesake but I have set up the code as below but only importing the first sheet, also is there a way to only need to enter the originating spreadsheet id once for the whole code instead of at each sheet section?
function ImportDataRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('AM trip'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('AM trip'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('PM trip'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('PM trip'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Pool / Beach'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('Pool / Beach'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Night Dive'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('Night Dive'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
}
Thank you for any help. Much appreciated.
The code can be simplified using a loop.
You said above that you have 5 sheets but you only have 4 in the code sample, add the other sheet name to var sheetNames.
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 to archive')
.addItem('Backup', 'dataBackup')
.addToUi();
}
function dataBackup() {
var inputSS = SpreadsheetApp.openById('1wglM4-5jx873vwtFRPVgC1qk27JjrsYjDwp0fNpl5Xg'); // The file ID of the Input sheet
var archiveSS = SpreadsheetApp.openById('146WU8RghfFqlCpCSX7n6kBAKOyxcpVKt14yhVfvYz-g'); // The file ID of the archive sheet
// The names of the sheets to be copied.
// NOTE: These names must match the names of the sheets in BOTH spreadsheets i.e. the same case the spelling and the same spaces.
var sheetNames = ['AM trip', 'PM trip', 'Pool / Beach', 'Night Dive'];
for (var i = 0; i < sheetNames.length; i++) { // Loop to each sheet listed in 'var sheetNames' and copy the data.
var inputSheet = inputSS.getSheetByName(sheetNames[i]);
var archiveSheet = archiveSS.getSheetByName(sheetNames[i]);
var data = inputSheet.getRange('B7:U38').getValues();
archiveSheet.getRange(archiveSheet.getLastRow() + 1, 2, data.length, data[0].length).setValues(data);
}
}