Target range coordinates outside sheet dimensions - google-apps-script

The following code used to work just fine to move rows from one sheet to another based on a criteria in column 7 of the source row.
Now I get the error message that "The coordinates of the target range are outside the dimensions of the sheet."
Would greatly appreciate any help!
Thanks!
function moveDispos() {
Logger.clear();
var ss = SpreadsheetApp.openById("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
var sheet = ss.getSheetByName("XXXXXXX");
var startRow = 2;
var lastRow = sheet.getLastRow();
var ColToWatch = 7;
var moves = [];
var data = sheet.getDataRange().getValues();
for (var i = startRow - 1; i <= lastRow - 1; i++) {
var rowVal = data[i][6];
if (rowVal === "XXXXX") {
moves.push(Number(i + 1));
}
}
var moveFrom = "XXXXXXX";
var moveTo = "XXXX XXXXXX";
var sourceSheet = ss.getSheetByName(moveFrom);
var targetSheet = ss.getSheetByName(moveTo);
for (var i = 0; i < moves.length; i++) {
move(moves[i] - i, sourceSheet, targetSheet);
}
}
function move(sourceRow, sourceSheet, targetSheet) {
var sourceRange = sourceSheet.getRange(sourceRow, 1, 1, sourceSheet.getLastColumn());
var targetRange = targetSheet.getRange(targetSheet.getDataRange().getLastRow() + 1, 1);
sourceRange.copyTo(targetRange);
sourceSheet.deleteRow(sourceRow);
}

Related

Google script function works when it runs separately, but when I run all the functions together it doesn't work

I've got a question about my script. I've got a script where when running together only 2 out of 3 functions run, the other function does not run. The one function that does not work when running together is the "medewerkerLijst" function. When I run that function on it's own it works perfectly as it should, when running it together with the other 2 functions it does not work. I can't figure out why it's not working.
The functions "nieuwDossier" & "sorteerDossiers" do work when running together. Sorry if my code is a mess, I'm still learning how to code.
function Aanmaken(){
medewerkerLijst();
nieuwDossier();
sorteerDossiers();
}
function medewerkerLijst() {
var ss5 = SpreadsheetApp.getActiveSpreadsheet();
var copySheet2 = ss5.getSheetByName("-Dossier");
var pasteSheet2 = ss5.getSheetByName("-Medewerkers");
// get source range
var source2 = copySheet2.getRange(2,7,1,1);
// get destination range
var destination2 = pasteSheet2.getRange(pasteSheet.getLastRow()+1,1,1,1);
// copy values to destination range
source2.copyTo(destination2, {contentsOnly:true});
}
function nieuwDossier() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheetByName('-Template');
sheet.copyTo(source).setName('Nieuw');
var sheet2 = source.getSheetByName('-Dossier');
var cell = sheet2.getRange("G2");
var value = cell.getValue();
var sheet3 = source.getSheetByName('Nieuw');
sheet3.setName(value);
sheet3.setTabColor(null);
}
function sorteerDossiers () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameArray = [];
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
sheetNameArray.push(sheets[i].getName());
}
sheetNameArray.sort();
for( var j = 0; j < sheets.length; j++ ) {
ss.setActiveSheet(ss.getSheetByName(sheetNameArray[j]));
ss.moveActiveSheet(j + 1);
}
}
This runs just fine
function main() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName("Sheet1");
var sh2 = ss.getSheetByName("Sheet2");
var rg1 = sh1.getRange(2, 7, 1, 1);
var rg2 = sh2.getRange(sh2.getLastRow() + 1, 1, 1, 1);
rg1.copyTo(rg2, { contentsOnly: true });
var sh3 = ss.getSheetByName('Sheet3');
var sh4 = sh3.copyTo(ss).setName('Sheet4');
var cell = sh1.getRange("G2");
var v1G2 = cell.getValue();
sh4.setName(v1G2);
sh4.setTabColor(null);
var shts = ss.getSheets().map(sh => sh.getName()).sort().forEach((n, i) => {
ss.setActiveSheet(ss.getSheetByName(n));
ss.moveActiveSheet(i + 1)
});
}

Iterate through column A and do until empty cell

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

data transfert based on cell value on google sheet

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

Google App Scripts: How do you copy a row to the next available row in a new tab depending on the value in a cell

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

Loop to disable function in onEdit script

I have a script that adds specific values to the last row of the sheet by clicking a checkbox (has yes/no values) and deletes the row by unchecking the same checkbox. It works fine
but as it is onEdit script - it keeps on going adding values. I am trying to loop all this job to check if the value already exists and if yes - do nothing. I developed the following but it does not work - keeps adding values with every edit. Here is the code
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getActiveSheet()
var paramrange = sheet.getRange('A55:C60')
var destn = sheet.getRange(sheet.getLastRow()+1, 1)
var destnorma = sheet.getRange(sheet.getLastRow()+1, 2)
var orpac = sheet.getRange('E23')
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[0] == 'ORP Ac') {
} else{
if(orpac.getValue() == 'yes') {
var orpacn = 'ORP Ac';
var orpacnorm = '-150';
destn.setValue(orpacn)
destnorma.setValue(orpacnorm)
} else
{
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[0] == 'ORP Ac') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}}
}
}}
Your second loop is inside the first loop and it uses the same index. That's a problem.
This might work better. But I can't tell since I don't have your data.
function onEdit() {
var ss=SpreadsheetApp.getActive()
var sheet=ss.getActiveSheet()
var paramrange=sheet.getRange('A55:C60')
var destn=sheet.getRange(sheet.getLastRow()+1, 1)
var destnorma=sheet.getRange(sheet.getLastRow()+1, 2)
var orpac=sheet.getRange('E23')
var orpacvalue=orpac.getValue();
var rows=sheet.getDataRange();
var numRows=rows.getNumRows();
var values=rows.getValues();
for (var i=0;i<numRows;i++) {
var row=values[i];
if (row[0]!='ORP Ac') {
if(orpacvalue=='yes') {
var orpacn = 'ORP Ac';
var orpacnorm = '-150';
destn.setValue(orpacn)
destnorma.setValue(orpacnorm)
} else
{
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var j = 0; j<numRows; j++) {
var row = values[j];
if (row[0] == 'ORP Ac') {
sheet.deleteRow((parseInt(j)+1) - rowsDeleted);
rowsDeleted++;
}
}
}
}
}
}
This appears to be a lot of script for a simple trigger which needs to complete in less than 30 seconds.