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');
}
Related
My script is working but it times out. I am unsure as to what I can do to keep it from doing this.
function archived(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName('IVA');
const targetSheet = e.source.getSheetByName('Archived Videos');
const numColumnsToMove = 20;
var sheetTo = SpreadsheetApp.getActive().getSheetByName('IVA');
var myValues = sheetTo.getRange('A:A').getValues();
var i;
for (i = 0; i < myValues.length; i++) {
if (myValues[i][0] === '')
return i + 1;
const rangeToMove = sheet.getRange(e.range.rowStart, 1, 1, numColumnsToMove);
const values = rangeToMove.getValues();
appendRowsV(targetSheet, values, 1);
sheet.deleteRows(e.range.rowStart, 1);
var lRow = sheet.getLastRow();
var lCol = sheet.getLastColumn(), range = sheet.getRange(lRow,1,1,lCol);
sheet.insertRowsAfter(lRow, 1);
range.copyTo(sheet.getRange(lRow+1, 1, 1, lCol), {contentsOnly:false});
}
}
As an FYI my sheet has 1500 rows and 15 columns. don't know if that is important or not.
Just in case. As far as I can tell from your code you're trying to append all data from the sheet 'IVA' at the bottom of the sheet 'Archived Videos'. It can be done this way:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var src_range = ss.getSheetByName('IVA').getDataRange();
var dest_sheet = ss.getSheetByName('Archived Videos');
var last_row = dest_sheet.getLastRow() + 1;
var dest_range = dest_sheet.getRange('A' + last_row);
src_range.copyTo(dest_range);
}
I'm not good with coding and i tried to mix some codes that i found and made what i needed, but now i face a difficult problem for me, so I'm trying when to press the button that i created to run the script to send the data that have for example an 'X' in the last column. i successfully made the sending from one sheet to another but it send all the data for now i want to be sent only the chosen ones with an 'X'.
any help I'll be grateful.
this is my code
thank you
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var url = "https://docs.google.com/spreadsheets/d/15nIAXcP0a14OvvBr5lww4tO23stQD4PEu0QDAcgaFyE/edit#gid=0";
var ss2 = SpreadsheetApp.openByUrl(url);
var pasteSheet = ss2.getSheetByName("Sheet2");
// get source range
var max = copySheet.getMaxRows().toString();
var range = copySheet.getRange(2,1,max,4);
var dataValues = range.getValues();
for(var i = 1; i < dataValues.length; i++)
{
if(dataValues[i][5] === 'X')
{
copySheet.appendRow([dataValues[i][0],
dataValues[i][1],
dataValues[i][2],
dataValues[i][3],
dataValues[i][4]]);
}
}
for(var i = 1; i < dataValues.length; i++)
{
if(dataValues[i][5] === 'X')
{
var clearRow = i+1;
dataSheet.getRange('A' + clearRow + ':F' + clearRow).clear();
}
}
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,max,1);
// clear source values
Browser.msgBox('Commande Confirmer');
}
This is all that your function does
dataSheet is undefined so I assume it was csh
function lfunko() {
const ss = SpreadsheetApp.getActive();
const csh = ss.getSheetByName("Sheet1");
const ss2 = SpreadsheetApp.openByUrl("ssurl");
const psh = ss2.getSheetByName("Sheet2");
var vs = csh.getRange(2, 1, csh.getLastRow() - 1, csh.getLastColumn()).getValues();
vs.forEach((r,i) => {
if (vs[i][5] === 'X') {
csh.appendRow([vs[i][0], vs[i][1], vs[i][2], vs[i][3], vs[i][4]]);
csh.getRange(i + 1, 1, 1, 6).clear();
}
});
}
I am trying to transfer a row (range A:AE) from sheet NSI to the next available row in Sheet1 if cell AE= 1.
My code doesn't seem to work, please could somebody help me?
Thank you!!
function myFunction() {
function copyRows() {
var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("NSI");
var lastRow = srcSheet.getLastRow();
for (var i = 2; i <= lastRow; i++) {
var srcRange = srcSheet.getRange("A" + i + ":AE" + i);
var cell = srcSheet.getRange("AE" + i);
var val = cell.getValue();
//sets the target sheet depending on the exam in column AE
if (val == "1") {
var tarSheet = sSheet.getSheetByName("Sheet1");
}
//insets the row in the correct target worksheet
var tarRow = tarSheet.getLastRow()+1;
tarSheet.insertRows(tarRow);
var tarRange = tarSheet.getRange("A" + (tarRow) + ":AE" + (tarRow));
srcRange.copyTo(tarRange);
}
};
}
function copyRows() {
var sSheet = SpreadsheetApp.getActive();
var srcSheet = sSheet.getSheetByName("NSI");
const vs = srcSheet.getRange(2, 1, srcSheet.getLastRow() - 1, 31).getDisplayValues();
var tarSheet = sSheet.getSheetByName("Sheet1");
vs.forEach(r => {
if (r[30] == "1") {
tarSheet.appendRow(r);
}
});
}
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
I am desperately looking for some help about an error I keep getting when running a function on a Google Spreadsheet.
I have written all the code and I get two situations:
if I run the script from the sheet where it is supposed to work, I get the error "Oops
We're sorry. The server encountered an error. Please press "OK" to refresh the sheet" even though all the operations are actually completed below the error popup.
if I run the script from another sheet the script is completed successfully.
I thought this problem could have been related to the active sheet function, so I started debugging to find a solution. I noticed I get absolutely no problems with all of the code except a part in which I ask the spreadsheet to make some operations on the columns: first to create a new one, then to resize two of them and eventually to delete one.
I am attaching all the code so that you can get a better idea of what the script is doing.
Thank you for your attention.
Tony
function forecastSettimanale() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var data = sheet.getDataRange();
// I need to operate on a column that has TOT written on its top, and its
// position can vary
var k = 1;
while (k <= data.getNumColumns()) {
var startRange = sheet.getRange(1, k, 1);
var value = startRange.getValue();
if(value == 'TOT') {
var flagColumn = startRange.getColumn(); //
}
k++;
}
sheet.insertColumnsBefore(flagColumn, 1);
sheet.setColumnWidth(flagColumn - 1, 40);
sheet.setColumnWidth(flagColumn, 50);
sheet.deleteColumn(flagColumn - 6);
sheet.setColumnWidth(flagColumn, 100);
}
NEW edited code with your suggestion:
function forecastSettimanale() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var data = sheet.getDataRange();
var searchElement = 'TOT';
var firstRow = 1, firstColumn = 1, rows = 1;
var lastColumn = sheet.getLastColumn();
var lastRow = sheet.getLastRow();
var range = sheet.getRange(firstRow, firstColumn, rows, lastColumn).getValues();
var flagColumn = range[0].lastIndexOf(searchElement) + 1;
sheet.setColumnWidth(flagColumn - 1, 40);
sheet.deleteColumn(flagColumn - 6);
sheet.insertColumns(flagColumn - 1);
var cellLeggiData = sheet.getRange(2, 3, 1);
var cellData = sheet.getRange(1, flagColumn - 1, 1);
cellData = cellData.setValue(cellLeggiData.getValue());
var last_row = sheet.getLastRow();
var copiaDa = sheet.getRange(2, flagColumn, last_row - 1);
var copiaA = sheet.getRange(2, flagColumn - 1, last_row - 1);
copiaDa.copyTo(copiaA, {contentsOnly: true});
for (var i = 35; i <= lastRow; i+=33) {
var cellaFormula = sheet.getRange(i, flagColumn, 1);
var rigaPrecedente = i - 1;
var totN = sheet.getRange(rigaPrecedente, flagColumn, 1);
var totFore = sheet.getRange(rigaPrecedente, flagColumn - 1, 1);
var letteraColonna = String.fromCharCode(64 + flagColumn);
var letteraColonnaMenoUno = String.fromCharCode(63 + flagColumn);
cellaFormula.setFormula("=" + letteraColonnaMenoUno + rigaPrecedente + "-" + letteraColonna + rigaPrecedente);
}
sheet.setColumnWidth(flagColumn - 1, 100);
sheet.showColumns(flagColumn - 6, 6);
}
I can not reproduce the problem. The code in question works without problem, as you indicate. Maybe you have to do some validations such as, for example, in sheet.deleteColumn(flagColumn - 6); What if flagColumn is less than 6?
I modified a bit the way to get the column that is needed.
/* CODE FOR DEMONSTRATION PURPOSES */
function forecastSettimanale() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var searchElement = 'TOT';
var firstRow = 1, firstColumn = 1, rows = 1;
var lastColumn = sheet.getLastColumn();
var range = sheet.getRange(firstRow, firstColumn, rows, lastColumn).getValues();
var flagColumn = range[0].lastIndexOf(searchElement);
if (flagColumn > -1) {
++flagColumn;
sheet.insertColumnsBefore(flagColumn, 1);
sheet.setColumnWidth(flagColumn - 1, 40);
sheet.setColumnWidth(flagColumn, 50);
sheet.deleteColumn(flagColumn - 6);
sheet.setColumnWidth(flagColumn, 100);
}
}
Try to debug line by line to determine the method generates the error, that way it will be easier to find a solution.