I have a code which copies row if condition. By the source sheet is around 8 000 lines and the script fails because is too long.
How can I speed it up?
I would like it takes one 1 hr max.
I think it makes too many calls to Google. I hope we can reduce them.
function dispacthtosheets() {
var sSheet = SpreadsheetApp.openById('xxxxxx');
var srcSheet = sSheet.getSheetByName ( "Dispatch" );
var lastRow = srcSheet.getLastRow ();***emphasized text***
for ( var i = 1 ; i <= lastRow ; i ) {
var cellule = srcSheet.getRange ( "L" i );
var val = cellule.getValue();
if ( val == "Changement ENT" ) {
var tarSheet3 = sSheet.getSheetByName ( "Changement ENT" );
var srcRange = srcSheet.getRange ( "C" i ":R" i );
var tarRow3 = tarSheet3.getLastRow ();
var tarRange3 = tarSheet3.getRange ( "C" tarRow3 ":R" tarRow3);
srcRange.copyTo (tarRange3);
tarSheet3.insertRowAfter ( tarRow3 );
} if ( val == "VC en REL" ) {
var tarSheet1 = sSheet.getSheetByName ( "VC en REL" );
var srcRange = srcSheet.getRange ( "C" i ":R" i );
var tarRow1 = tarSheet1.getLastRow ();
var tarRange1 = tarSheet1.getRange ( "C" tarRow1 ":R" tarRow1);
srcRange.copyTo (tarRange1);
tarSheet1.insertRowAfter ( tarRow1 );
} if ( val == "Solo à étudier" ) {
var tarSheet2 = sSheet.getSheetByName ( "Solo à étudier" );
var srcRange = srcSheet.getRange ( "C" i ":R" i );
var tarRow2 = tarSheet2.getLastRow ();
var tarRange2 = tarSheet2.getRange ( "C" tarRow2 ":R" tarRow2);
srcRange.copyTo (tarRange2);
tarSheet2.insertRowAfter ( tarRow2 );
} if ( val == "VC 6 mois" ) {
var tarSheet4 = sSheet.getSheetByName ( "VC 6 mois" );
var srcRange = srcSheet.getRange ( "C" i ":R" i );
var tarRow4 = tarSheet4.getLastRow ();
var tarRange4 = tarSheet4.getRange ( "C" tarRow4 ":R" tarRow4);
srcRange.copyTo (tarRange4);
tarSheet4.insertRowAfter ( tarRow4);
} if ( val == "Sortie Nat" ) {
var tarSheet5 = sSheet.getSheetByName ( "Sortie Nat" );
var srcRange = srcSheet.getRange ( "C" i ":R" i );
var tarRow5 = tarSheet5.getLastRow ();
var tarRange5 = tarSheet5.getRange ( "C" tarRow5 ":R" tarRow5);
srcRange.copyTo (tarRange5);
tarSheet5.insertRowAfter ( tarRow5 );
} if ( val == "ZCTM" ) {
var tarSheet6 = sSheet.getSheetByName ( "ZCTM" );
var srcRange = srcSheet.getRange ( "C" i ":R" i );
var tarRow6 = tarSheet6.getLastRow ();
var tarRange6 = tarSheet6.getRange ( "C" tarRow6 ":R" tarRow6);
srcRange.copyTo (tarRange6);
tarSheet6.insertRowAfter ( tarRow6 );
} if ( val == "Appro validée" ) {
var tarSheet7 = sSheet.getSheetByName ( "Appro validée" );
var srcRange = srcSheet.getRange ( "C" i ":R" i );;
var tarRow7 = tarSheet7.getLastRow ();
var tarRange7 = tarSheet7.getRange ( "C" tarRow7 ":R" tarRow7);
srcRange.copyTo (tarRange7);
tarSheet7.insertRowAfter ( tarRow7 );
} if ( val == "F943" ) {
var tarSheet8 = sSheet.getSheetByName ( "F943" );
var srcRange = srcSheet.getRange ( "C" i ":R" i );
var tarRow8 = tarSheet8.getLastRow ();
var tarRange8 = tarSheet8.getRange ( "C" tarRow8 ":R"
tarRow8);
srcRange.copyTo (tarRange8);
tarSheet8.insertRowAfter ( tarRow8 );
}
sSheet.toast(' VC dispacthé !', 'Confirmation