I have found so many scripts to copy values only from one spreadhseet to another. However, all of them is to copy the whole spreasheet.
I am very new with google script and cannot find a way to copy values only from specific tabs to another spreasheet adding these new tabs to it.
function temp() {
var sss = SpreadsheetApp.openById('XYZ'); // sss = source spreadsheet
//var ss = sss.getSheets()[4]; // ss = source sheet
var ss = sss.getSheets(); // ss = source sheet
var id=4; //default number
for(var i in ss)
{
var sheet = ss[i];
if(sheet.getName()== "ABC")
{ id=i;
break;
}
}
console.log(id);
ss=sss.getSheets()[id];
//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();
SpreadsheetApp.flush();
var tss = SpreadsheetApp.getActiveSpreadsheet(); // tss = target spreadsheet
var ts = tss.getSheetByName('ABC'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
Thanks a lot in advance.
Issue:
You can to copy the values from a specific set of sheets to a new spreadsheet.
I assume that you don't want to copy the sheet itself, but only the values from the source sheet.
Modifications:
Use filter to get an array of your desired sheets (based on sheet name).
If the target spreadsheet doesn't have a sheet with that name, create it with Spreadsheet.insertSheet.
Code sample:
function temp() {
var sheetNames = ["ABC", "DEF"]; // Change accordingly
var sss = SpreadsheetApp.openById('XYZ');
var sheetsToCopy = sss.getSheets().filter(s => sheetNames.includes(s.getSheetName()));
sheetsToCopy.forEach(ss => {
var sourceSheetName = ss.getSheetName();
var SRange = ss.getDataRange();
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var tss = SpreadsheetApp.getActiveSpreadsheet();
var ts = tss.getSheetByName(sourceSheetName);
if (!ts) ts = tss.insertSheet(sourceSheetName);
ts.getRange(A1Range).setValues(SData);
});
}
Copy these sheets to another spreadsheet
function copythese() {
const ss = SpreadsheetApp.getActive();
const these = ['Sheet0','Sheet1'];//Put your sheetnames here
const another = SpreadsheetApp.openById("another id");//put the other spreadsheet id here
ss.getSheets().filter(sh => ~these.indexOf(sh.getName())).forEach(sh => {
sh.copyTo(another);
});
}
Append These Values to another spreadsheet
function appendthesevalues() {
const ss = SpreadsheetApp.getActive();
const these = ['Sheet0','Sheet1'];
const another = SpreadsheetApp.openById("another id");
ss.getSheets().filter(sh => ~these.indexOf(sh.getName())).forEach(sh => {
let vs = sh.getDataRange().getValues();
let nsh = another.getSheetByName(sh.getName());
if(!nsh) {
nsh = another.insertSheet(sh.getName());
SpreadsheetApp.flush();
}
if(nsh) {
nsh.getRange(nsh.getLastRow() + 1, 1 ,vs.length, vs[0].length).setValues(vs);
}
});
}
Related
I have read all the previous posts and I have a similiar subjetc that I can't solve. I have to copy/paste a Google Sheets (two sheets, 'GENERAL', 'VALEUR') document.
I have won to write a code to copy/paste : 1 document (source) ==to==> 1 document (destination)
function expCalc(){
copypaste_GENERAL();
copypaste_VALEUR();
}
function copypaste_GENERAL() {
var source_G = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw/edit#gid=0');
var sourceSheet_G = source_G.getSheetByName('GENERAL');
var sourceRange_G = sourceSheet_G.getDataRange();
var sourceValues_G = sourceRange_G.getValues();
var tempSheet_G = source_G.getSheetByName('TEMP_GENERAL');
var tempRange_G = tempSheet_G.getRange('A1:DU11');
var destination_G = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA/edit#gid=1580219321');
var destSheet_G = destination_G.getSheetByName('GENERAL');
sourceRange_G.copyTo(tempRange_G); // paste all formats?, broken references
tempRange_G.offset(0, 0, sourceValues_G.length, sourceValues_G[0].length)
.setValues(sourceValues_G); // paste all values (over broken refs)
copydSheet = tempSheet_G.copyTo(destination_G); // now copy temp sheet to another ss
copydSheet.getDataRange().copyTo(destSheet_G.getDataRange());
destination_G.deleteSheet(copydSheet); //delete copydSheet
}
function copypaste_VALEUR() {
var source_V = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw/edit#gid=0');
var sourceSheet_V = source_V.getSheetByName('VALEUR');
var sourceRange_V = sourceSheet_V.getDataRange();
var sourceValues_V = sourceRange_V.getValues();
var tempSheet_V = source_V.getSheetByName('TEMP_VALEUR');
var tempRange_V = tempSheet_V.getRange('A1:I255');
var destination_V = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA/edit#gid=1580219321');
var destSheet_V = destination_V.getSheetByName('VALEUR');
sourceRange_V.copyTo(tempRange_V); // paste all formats?, broken references
tempRange_V.offset(0, 0, sourceValues_V.length, sourceValues_V[0].length)
.setValues(sourceValues_V); // paste all values (over broken refs)
copydSheet = tempSheet_V.copyTo(destination_V); // now copy temp sheet to another ss
copydSheet.getDataRange().copyTo(destSheet_V.getDataRange());
destination_V.deleteSheet(copydSheet); //delete copydSheet
}
but I can't write a code to copy/paste : 1 document (source) ==to==> MANY (more than 1) documents (destination) according to a list of URLs (here, example only on 2 URLs)
Here is my test code (using only t'GENERAL' sheet for this present test)
function copypaste_GENERAL() {
var source = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw/edit#gid=0');
var sourceSheet = source.getSheetByName('GENERAL');
var sourceRange = sourceSheet.getDataRange();
var sourceValues = sourceRange.getValues();
var tempSheet = source.getSheetByName('TEMP_GENERAL');
var tempRange = tempSheet.getRange('A1:DU11');
var destSpreadUrl = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA/edit#gid=1580219321');
var destSheetUrl = destSpreadUrl.getSheetByName('URLTST');
var destSheet_G = destSpreadUrl.getSheetByName('GENERAL');
var urlessai = destSheetUrl.getRange("D2:D3").getValues();
for (var row = 1; row = 3; row++) {
if (urlessai[row] != '') {
sourceRange.copyTo(tempRange); // paste all formats?, broken references
tempRange.offset(0, 0, sourceValues.length, sourceValues[0].length)
.setValues(sourceValues); // paste all values (over broken refs)
copydSheet = tempSheet.copyTo(destSpreadUrl); // now copy temp sheet to another ss
copydSheet.getDataRange().copyTo(destSheet_G.getDataRange());
destSpreadUrl.deleteSheet(copydSheet); //delete copydSheet
};
};
};
Please, can you help me to find a solution to write this copy/paste loop on a list of URLs (for x users) ?
Thanks in advance !
Philippe
This is probably not exactly what you want but I think it's close and I'm willing to tweak it.
function copySrcDes() {
const ss = SpreadsheetApp.getActive();
const locs = [{srcid:"1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw",desid:"1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA",shts:["GENERAL"]},{srcid:"1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw",desid:"1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA",shts:["VALEUR"]}];
locs.forEach(obj => {
let sss = SpreadsheetApp.openById(obj.srcid);
let dss = SpreadsheetApp.openById(obj.desid);
obj.shts.forEach(n => {
let ssh = sss.getSheetByName(n);
let dsh = dss.getSheetByName(n);
let vs = ssh.getDataRange().getValues();
dsh.getRange(1,1,vs.length,vs[0].length).setValues(vs);
});
});
}
Is there a way to get the range of a unprotected cells of a sheet in A1Notation?
I have this function but don't show me the A1Notation, and I don't know what more do:
function parametrosdeProceso() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var hoja = ss.getSheetByName("KMD108");
var protection = hoja.protect();
var unprotected= protection.getUnprotectedRanges();
Logger.log(unprotected);
}
Try, for one sheet
function listOfUnprotectedRanges() {
var p = SpreadsheetApp.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
var ranges = p.getUnprotectedRanges().map(r => r.getA1Notation())
console.log(ranges)
}
for the whole spreadsheet
function listOfUnprotectedRanges() {
var p = SpreadsheetApp.getActiveSpreadsheet().getProtections(SpreadsheetApp.ProtectionType.SHEET);
p.forEach(x => {
var ranges = x.getUnprotectedRanges().map(r => r.getA1Notation())
console.log(ranges)
})
}
function saveData(){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var formSheet = spreadsheet.getSheetByName("Receipt");
var dataSheet = spreadsheet.getSheetByName("Historical Sales")
var fieldRange = \["B12:X"\]
var fieldValue = fieldRange.map(f =\>
formSheet.getRange(f).getValue())
//console.log(fieldValue)
dataSheet.appendRow(fieldValue)
};
function exp5() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('W12').activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.PREVIOUS).activate();
currentCell.activateAsCurrentCell();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Historical Receipts'), true);
spreadsheet.getRange('X963').activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.PREVIOUS).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
spreadsheet.getRange('A14:V23').activate();
spreadsheet.getRange('Receipt!B12:W21').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
This is how I'd do it:
//Not tested, but it can give you a direction
function moveData () {
var formSheet = spreadsheet.getSheetByName("Receipt");
var formRng = formSheet.getRange(); //Specify the data range here
var formData = formRng.getValues();
var dataSheet = spreadsheet.getSheetByName("Historical Sales");
//It gets the first empty row in column A and "pastes" the data set (form Data) .
datasheet.getRange(datasheet.getLastRow()+1,1,formData.length,formData[0].length).setValues(formData);
}
I have looked and looked and tried to adapt some scripts but am stuck.
Can someone help/show me how to add a collection of specific cells on one sheet and populate the next available row on a second sheet with these values? Please. This is what I have:
function AddToDB2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("PurchaseOrder");
var targetSheet = target.getSheetByName("DataBase");
var range = ss.getDataRange();
var values = range.getValues();
var date1 = values[1][6]; //these values are collected from multiple cells not in a specific row or col
var po = values[2][6];
var vendor = values[7][0];
var shipto = values[7][3];
//this was for testing that the values were grabbed correctly.
var data = [date1,po,vendor,shipto];
// I don't think this part is even close
var lastRow = targetSheet.getLastRow()
var rowData = values[0]
var newData = []
for(n=1;n<lastRow;++n){newData.push(rowData)}
targetSheet.getRange("C2:H"+lastRow).setValues(newData);
}
This is the other way I came up with:
function addToDB() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("PurchaseOrder");
var targetSheet = target.getSheetByName("DataBase");
var sourceRangePO = sourceSheet.getRange("G3");
var poValues = sourceRangePO.getValues();
if(poValues != "") {
var sourceRangeDate = sourceSheet.getRange("G2");
var targetRangeDate = targetSheet.getRange("A2");
var dateValues = sourceRangeDate.getValues();
targetRangeDate.setValues(dateValues);
var sourceRangePO = sourceSheet.getRange("G3");
var targetRangePO = targetSheet.getRange("B2");
var poValues = sourceRangePO.getValues();
targetRangePO.setValues(poValues);
var sourceRangeVendor = sourceSheet.getRange("A8");
var targetRangeVendor = targetSheet.getRange("C2");
var vendorValues = sourceRangeVendor.getValues();
targetRangeVendor.setValues(vendorValues);
var sourceRangeShipTo = sourceSheet.getRange("D8");
var targetRangeShipTo = targetSheet.getRange("D2");
var shipToValues = sourceRangeShipTo.getValues();
targetRangeShipTo.setValues(shipToValues);
targetSheet.insertRowAfter(1);
Browser.msgBox("Your PO has beed saved!");
}
else { Browser.msgBox("Please enter a PO number"); }
}
You were very close. Try this. I was a little confused by your setValues C:H. You only have 4 values to set. I set them in D-G. You can adjust as needed.
function AddToDB2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("PurchaseOrder");
var targetSheet = ss.getSheetByName("DataBase");
var range = sourceSheet.getDataRange();
var values = range.getValues();
var date1 = values[1][6]; //these values are collected from multiple cells not in a specific row or col
var po = values[2][6];
var vendor = values[7][0];
var shipto = values[7][3];
var data = [date1,po,vendor,shipto];
var lastRow = targetSheet.getLastRow()
var newData = []
for(n=0;n<data.length;++n){newData.push([data[n]])}
var rng =targetSheet.getRange(lastRow+1,1,1,4) // Col A-D
rng.setValues([newData]);
}
I have a script that copies a certain cell when a new form is submitted, (which contains a drop down list, on sheet "Validation") and pastes it into the first blank cell in column A on the sheet "Internal Recruitment Requests".
The code is not copying into the right cell.
Below is the current script, which doesn't work.
I am running it with onEdit at the moment for testing purposes.
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Validation');
var sheet2 = ss.getSheetByName('Internal Recruitment Requests');
var lastEmptyOnColumnA = sheet2.getRange("A1:A" + sheet2.getLastRow()).getValues().join(",").replace(/,,/g, '').split(",").length;
var DVcopyFrom = sheet1.getRange("A4");
var DVcopyTo = sheet2.getRange('A' + lastEmptyOnColumnA);
sheet1.getRange(DVcopyFrom).copyToRange(sheet2.getRange(DVcopyTo), {contentsOnly:false});
}
Bingo, all sorted. used the below script which was a tiny tweak on what you had given me. thanks for the help :)
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Validation');
var sheet2 = ss.getSheetByName('Internal Recruitment Requests');
var lastRowSheet2 = sheet2.getLastRow();
var cellRefLastRowSheet2 = "A1:A" + lastRowSheet2.toString();
var cellRefLastRowSheet2Empty = "A" + lastRowSheet2.toString();
var lastEmptyOnColumn_A_Sheet2 = sheet2.getRange(cellRefLastRowSheet2);
var DVcopyFromValue = sheet1.getRange("A4");
var contentOfLastCell = sheet2.getRange(cellRefLastRowSheet2Empty).getValue();
if (contentOfLastCell === "")
{
DVcopyFromValue.copyTo(sheet2.getRange(cellRefLastRowSheet2Empty));
}
}