small beginner here.
I need to do copy values from one column and paste them in another one.
Firstly, i try something like "saveHistoryColumn". It works, but I do not wanna write variables from A-ZZ......Therefore I was looking for some looping, "Test" worsk, but my question is how to place the original data from B--> A, D-->C etc. in one sheet ? The test copy value from sheet 1 to sheet at same position...
function saveHistoryColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var ss1 = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Sheet1!B15:B20");
var source2 = ss1.getRange ("Sheet1!D15:D20");
source.copyTo (ss.getRange ("Sheet1!A15:A20"), {contentsOnly: true});
source2.copyTo (ss1.getRange ("Sheet1!C15:C20"), {contentsOnly: true});
}
function test () {try {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("Sheet1");
var rlist = sheet.getRangeList(["B1:B10","D1:D10"]);
sheet = spread.getSheetByName("Sheet2"); // target will be Sheet1 A1:A10,C1:C10... etc.
for( var i=0; i<rlist.getRanges().length; i++ ) {
var r1 = rlist.getRanges()[i];
var r2 = sheet.getRange(r1.getA1Notation());
r1.copyTo(r2);
}} catch(err) {
Logger.log(err);}}
You can use something like
function backupColumns() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
for(col = 1; col < sheet.getLastColumn(); col+=2) {
var values = sheet.getRange(1, col + 1, sheet.getLastRow(), 1).getValues();
sheet.getRange(1, col, sheet.getLastRow(), 1).setValues(values);
}
}
to copy each even column into each odd if that's what you want
Related
I have two issues on my code, due to my luck of experience in coding, the code that I have is made to copy rows with a specific cell value, in my case it's 'commande confirmer', the first problem that I have is the first line of my values even if it has the specific value it's not copying but on other lines it's working perfectly.
And for the 2nd issue I want to get also when the data copied it transfer with it to the active sheet email, I found how to get it, but how to include it and being transferred I couldn't make it.
Here is my code
and thanks in advance.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var url = "https://docs.google.com/spreadsheets/d/15nIAXcP0a14OvvBr5lww4tO23stQD4PEu0QDAcgaFyE/edit?ouid=112360896426304156672&usp=sheets_home&ths=true";
var ss2 = SpreadsheetApp.openByUrl(url);
var pasteSheet = ss2.getSheetByName("Sheet2");
// get source range
var max = copySheet.getMaxRows().toString();
var email= Logger.log(Session.getActiveUser().getEmail());
var range = copySheet.getRange(2, 1, max, 10);
var dataValues = range.getValues();
for (i = 1; i < dataValues.length; i++) {
if (dataValues[i][9] === 'COMMANDE CONFIRMER') {
pasteSheet.appendRow([dataValues[i][0],
dataValues[i][email],
dataValues[i][1],
dataValues[i][2],
dataValues[i][3],
dataValues[i][4],
dataValues[i][5],
dataValues[i][6],
dataValues[i][7],
dataValues[i][8],
dataValues[i][9]]);
var clearRow = i + 2;
copySheet.getRange('A' + clearRow + ':J' + clearRow).clear();
}
}
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, max, 1);
// clear source values
Browser.msgBox('Commande Confirmer');
}
Try it this way:
function funko() {
var ss = SpreadsheetApp.getActive();
var csh = ss.getSheetByName("Sheet1");
var ss2 = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/15nIAXcP0a14OvvBr5lww4tO23stQD4PEu0QDAcgaFyE/edit?ouid=112360896426304156672&usp=sheets_home&ths=true");
var psh = ss2.getSheetByName("Sheet2");
var email = Session.getActiveUser().getEmail();
var range = csh.getRange(2, 1, csh.getLastRow() - 1, 10);
var cvs = range.getValues();
let oA = [];
cvs.forEach((r, i) => {
if (r[9] == 'COMMANDE CONFIRMER') {
let t = r.slice();
t.splice(1, 0, email);
oA.push(t);
csh.getRange(i + 1, 1, 1, csh.getLastColumn()).clearContent();
}
});
psh.getRange(psh.getLastRow() + 1, 1, oA.length, oA[0].length).setValues(oA);
Browser.msgBox('Commande Confirmer');
}
Hi there those way cleverer than me!
I have a Google script to export a pdf from a Google sheet which works fine. However I have an image in a cell that is not displayed in the exported pdf, instead it just says "CELLIMAGE". If I manually export the pdf it displays the image correctly...
My code is:
function exportFl() {
var sheetName = "FL"
//var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
//var allsheets = ss.getSheets();
//for (var s in allsheets){
//var sheet=allsheets[s]
//var maxRows = ss.getMaxRows();
//var lastRow = ss.getLastRow();
//if (maxRows-lastRow != 0){
//ss.deleteRows(lastRow+1, maxRows-lastRow);}
var value = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange('E7').getValue();
var folderID = "1x0tBqOP07e2XIzXHgvYqAaBiu7Zd7DfV"; // Folder id to save in a folder.
var timeZone = Session.getScriptTimeZone();
date = Utilities.formatDate(new Date(), timeZone, "YYMMdd");
var pdfName = value+"_FSL Conformance_"+ date;
var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
var folder = DriveApp.getFolderById(folderID);
//Copy whole spreadsheet
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder))
//delete redundant sheets
var sheets = destSpreadsheet.getSheets();
for (i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName){
destSpreadsheet.deleteSheet(sheets[i]);
}
}
var destSheet = destSpreadsheet.getSheets()[0];
//repace cell values with text (to avoid broken references)
var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns());
var sourcevalues = sourceRange.getValues();
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
destRange.setValues(sourcevalues);
//save to pdf
// --- I added below script.
var r = destSheet.getDataRange();
var startRow = r.getNumRows() + 1;
var number = destSheet.getMaxRows() - startRow + 1;
if (number > 0) destSheet.deleteRows(startRow, number);
SpreadsheetApp.flush(); // This might not be required to be used.
// ---
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var newFile = folder.createFile(theBlob);
//Delete the temporary sheet
DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);
}
Anyone got any ideas??
Thanks
It seems that in your script, the cells are overwritten by destRange.setValues(sourcevalues);. I thought that this might be the reason of your issue. And, in your script, the values have already been copied by var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder)). So in this case, how about the following modification?
In this modification, I used the following flow.
Copy the 1st tab.
Overwrite the data to the copied sheet.
Copy the images in the cells to the copied sheet.
Delete the temporal sheet.
When this flow is reflected to your script, it becomes as follows.
Modified script:
From:
var destSheet = destSpreadsheet.getSheets()[0];
//repace cell values with text (to avoid broken references)
var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns());
var sourcevalues = sourceRange.getValues();
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
destRange.setValues(sourcevalues);
To:
var tempSheet = destSpreadsheet.getSheets()[0];
// 1. Copy the 1st tab.
var destSheet = tempSheet.copyTo(destSpreadsheet);
// 2. Overwrite the data to the copied sheet.
var sourceRange = sourceSheet.getRange(1, 1, sourceSheet.getMaxRows(), sourceSheet.getMaxColumns());
var sourcevalues = sourceRange.getValues();
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
destRange.setValues(sourcevalues);
// 3. Copy the images in the cells to the copied sheet.
sourcevalues.forEach((r, i) => {
r.forEach((c, j) => {
if (c.toString() == "CellImage") {
tempSheet.getRange(i + 1, j + 1).copyTo(destSheet.getRange(i + 1, j + 1), {contentsOnly: true}); // Modified
}
});
});
// 4. Delete the temporal sheet.
destSpreadsheet.deleteSheet(tempSheet);
References:
copyTo(spreadsheet) of Class Sheet
copyTo(destination) of Class Range
I am trying to copy and paste "A4:J4" and "A5:H5" but I am getting an error because A5 is merged. Also "STAGE 3" only pastes in "G4" in Sheet2. How can I copy and paste this data to Sheet2 with merged cells?
function hi()
{
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var yourRange = sheet.getRange("A4");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var destSheet = ss.getSheetByName('Sheet2');
var yourLastColumn=sheet.getRange("A4:4").getLastColumn();
var range = ss.getRangeByName("A4");
var yourLastRange=sheet.getRange("A4").offset(0, yourLastColumn-1);
var rsltRange1 = yourLastRange.getNextDataCell(SpreadsheetApp.Direction.PREVIOUS);
var rsltRange2 = sheet.getRange("A4:" + rsltRange1.getA1Notation());
if (rsltRange2.isPartOfMerge()) {
var rangeTest = rsltRange2.getMergedRanges()[0];
} else {
}
Logger.log(rangeTest.getA1Notation());
var destRange = destSheet.getRange("A4");
rsltRange2.copyTo(destRange, {contentsOnly: false});
}
Here is a simplified version of your code that should copy your range correctly no matter if the range is merged or not:
function hi(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var destSheet = ss.getSheetByName('Sheet2');
var startColumn=1;
var startRow=4;
var numRows=2;
var numColumns=sheet.getLastColumn()-startColumn+1;
var range=sheet.getRange(startRow, startColumn, numRows, numColumns);
var destRange = destSheet.getRange("A4");
range.copyTo(destRange);
}
I am trying to copy a certain area into the next empty row of another spreadsheet.
Right now I am only able to copy it to a certain destination.
This is the code I am currently using:
function myFunction() {
var sheet=SpreadsheetApp.getActiveSheet();
var source="A3:S3";
var destination="Historie!B3";
sheet.getRange(source).copyTo(sheet.getRange(destination));
}
try something like this:
function xxx() {
var reference = 'sheet1!A2';
var rng = SpreadsheetApp.getActiveSpreadsheet().getRange(reference);
rng.setValue(rng.getValue()+1);
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Sheet1!A3:S3");
var destSheet = ss.getSheetByName("Historie");
var values = source.getValues().filter(function(e) {return e.some(function(f) {return f})});
destSheet.getRange(destSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
I am trying to get the source rows that match my conditions to move to a target sheet. Then delete the source rows in the source sheet. Please see below code. Any help would be much appreciated.
function moveDeleteRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheets()[0];
var values = s1.getDataRange().getValues();
var activeUser = Session.getActiveUser().getEmail();
var userFullName = ContactsApp.getContact(activeUser).getFullName();
Logger.log(userFullName);
var deleted = 0; // Counter
var cell = s1.getActiveCell();
for (var i = 0; i < values.length; i++) {
if (values[i][0] == userFullName && values[i][2] == 'Shipped') {
var rowdata = values[i];
Logger.log(rowdata);
var targetSheet = ss.getSheets()[2];
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s1.getRange(1, 1, 1, s1.getLastColumn()).moveTo(targetRange);
s1.deleteRow(i + 1 - deleted);
deleted++;
}
}
SpreadsheetApp.flush();
};
This is a general code for moving range to range
function moveRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheets()[0];
var sourceRange = sourceSheet.getDataRange();
var targetSheet = ss.getSheets()[1];
var targetRange = targetSheet.getRange(1, 1, sourceSheet.getLastRow(), sourceSheet.getLastColumn());
sourceRange.moveTo(targetRange) ;
}
https://developers.google.com/apps-script/reference/spreadsheet/range#movetotarget
You just need to select the source and target properly.
Edit: If you're moving specific rows and want to delete the rows as well then it would be better to push the target values into an array (set them all at once later using range.setValues(array[][])) and also the indexes for the rows to be deleted and then delete the rows using sheet.deleteRow(position) using another loop. While using this, you'll have to keep in mind the offset for the next row being deleted because the ones that were deleted will cause the rest to shift up.