J'essaie de mettre à jour ou de remplacer le contenu d'une ligne dans PasteSheet si la colonne B contient une valeur qui correspond à une autre valeur dans CopySheet Colonne B. La ligne correspondante dans Copysheet est copiée pour mettre à jour la ligne dans PasteSheet. Les valeurs des colonnes sont des nombres, c'est-à-dire 4.0

Voici le code sur lequel j'ai travaillé, mais rien ne se passe.

function timestamp(){

    var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("IMPORT");
  var pasteSheet = ss.getSheetByName("Dashboard");

  var lastRow = copySheet.getLastRow()
  var lastRow2 = pasteSheet.getLastRow()

  for (var i = 1; i <= lastRow2; i++){

    // pastesheet values

    var pasteRange = pasteSheet.getRange([i], 2);
    var pasteValues = pasteRange.getValues();

    var newRange = pasteSheet.getRange([i], 1,1,41);
    var newValues = newRange.getValues();

    for (var u = 1; u <= lastRow; u++){

    // copysheet values

    var copyRange = copySheet.getRange([u], 2);
    var copyValues = copyRange.getValues();

    var upRange = copySheet.getRange([u], 1,1,41);
    var upValues = upRange.getValues();

    if (copyValues == pasteValues){ // 
      newRange.setValue(upValues);

      }
    }
    }
}

Toute aide pour résoudre ce problème sera appréciée.

0
Smith O. 10 févr. 2020 à 19:08

1 réponse

Meilleure réponse

Mise à jour des lignes du tableau de bord à partir de la feuille d'importation qui correspondent aux dates de la colonne B

//V8
function updateMatching() {
  const ss=SpreadsheetApp.getActive();
  const srcsr=4;
  const dessr=4;
  const srcsh=ss.getSheetByName('IMPORT');
  const srcrg=srcsh.getRange(srcsr,1,srcsh.getLastRow()-srcsr+1,srcsh.getLastColumn());
  const srcvA=srcrg.getValues();
  const dessh=ss.getSheetByName('Dashboard');
  const desrg=dessh.getRange(dessr,1,dessh.getLastRow()-dessr+1,dessh.getLastColumn());
  const desvA=desrg.getValues();
  var oA=desvA;
  srcvA.forEach(function(sr,i){
    desvA.forEach(function(dr,j){
      if(sr[1]==dr[1]) {
        oA[j]=srcvA[i];
        dessh.getRange(dessr + j,1,1,dessh.getLastColumn()).setBackground("#ffff00");//Changed the background just highlight the updates.  You can remove this.
      }
    });
  });
  desrg.setValues(oA);
}


//Pre V8 - not much change.  I'm still learning es6 so if you want to offer suggestions to that end I'm open to them.
function updateMatching1() {
  var ss=SpreadsheetApp.getActive();
  var srcsr=4;
  var dessr=4;
  var srcsh=ss.getSheetByName('IMPORT');
  var srcrg=srcsh.getRange(srcsr,1,srcsh.getLastRow()-srcsr+1,srcsh.getLastColumn());
  var srcvA=srcrg.getValues();
  var dessh=ss.getSheetByName('Dashboard');
  var desrg=dessh.getRange(dessr,1,dessh.getLastRow()-dessr+1,dessh.getLastColumn());
  var desvA=desrg.getValues();
  srcvA.forEach(function(sr,i){
    desvA.forEach(function(dr,j){
      if(sr[1]==dr[1]) {
        desvA[j]=srcvA[i];
        dessh.getRange(dessr + j,1,1,dessh.getLastColumn()).setBackground("#ffff00")
      }
    });
  });
  desrg.setValues(desvA);
}

IMPORTER les données de début de feuille (csv) :

    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
HDR1,HDR2,HDR3,HDR4,HDR5,HDR6,HDR7,HDR8,HDR9,HDR10,HDR11,HDR12,HDR13,HDR14,HDR15,HDR16,HDR17,HDR18,HDR19,HDR20,HDR21,HDR22,HDR23,HDR24,HDR25,HDR26,HDR27,HDR28,HDR29,HDR30,HDR31,HDR32,HDR33,HDR34,HDR35,HDR36,HDR37,HDR38,HDR39,HDR40,HDR41
Fri Jan 31 2020 00:00:00 GMT-0700 (Mountain Standard Time),1,1,2,1,2,0,2,5,5,4,10,11,11,12,9,5,8,17,18,19,1,1,12,11,9,16,6,26,14,19,15,25,16,0,29,31,20,4,23,0
Sat Feb 01 2020 00:00:00 GMT-0700 (Mountain Standard Time),3,0,2,4,4,4,6,0,7,4,2,12,9,6,7,16,10,16,17,19,3,3,17,23,23,18,12,17,3,28,27,0,2,18,16,0,11,14,32,6
Sun Feb 02 2020 00:00:00 GMT-0700 (Mountain Standard Time),5,2,3,5,1,0,5,6,10,7,3,9,7,6,8,4,9,2,18,4,22,14,5,25,0,12,25,7,12,29,4,3,10,8,31,17,38,28,21,39
Mon Feb 03 2020 00:00:00 GMT-0700 (Mountain Standard Time),7,1,4,4,7,1,0,4,0,9,9,14,3,5,16,7,7,9,3,8,13,13,0,22,24,14,20,6,20,16,5,19,11,31,3,14,19,34,26,36
Tue Feb 04 2020 00:00:00 GMT-0700 (Mountain Standard Time),9,4,2,4,6,0,3,10,9,1,14,11,6,10,9,3,4,12,11,23,21,14,21,27,6,10,10,28,13,7,14,16,29,10,14,33,32,37,10,25
Wed Feb 05 2020 00:00:00 GMT-0700 (Mountain Standard Time),11,6,1,3,9,8,9,8,1,1,6,5,2,11,15,5,1,4,22,24,11,24,0,28,7,5,18,20,13,7,33,25,21,34,4,12,40,13,24,14
Thu Feb 06 2020 00:00:00 GMT-0700 (Mountain Standard Time),13,2,8,5,4,4,11,13,3,5,10,8,3,18,19,21,0,23,6,20,2,24,19,4,26,21,6,1,17,0,17,18,32,16,21,38,3,24,37,29
Fri Feb 07 2020 00:00:00 GMT-0700 (Mountain Standard Time),15,4,1,3,8,0,11,11,5,14,2,11,17,2,14,3,16,4,19,23,2,2,18,20,11,9,20,29,13,7,5,26,39,3,5,1,15,6,23,4
Sat Feb 08 2020 00:00:00 GMT-0700 (Mountain Standard Time),17,2,9,2,8,3,9,11,16,0,4,9,6,19,16,6,7,3,3,18,2,4,26,13,29,31,8,23,22,8,38,0,2,10,3,34,12,4,41,34
Sun Feb 09 2020 00:00:00 GMT-0700 (Mountain Standard Time),19,0,1,0,3,2,8,3,15,8,18,17,13,20,6,11,4,7,16,2,12,13,4,30,30,8,14,36,32,27,0,6,5,26,35,39,0,39,42,28
Mon Feb 10 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,9,8,2,14,14,1,11,4,0,2,12,13,13,2,12,23,8,28,14,1,23,18,33,33,7,17,12,16,21,24,5,20,12,29,39,3,28,28,26
Tue Feb 11 2020 00:00:00 GMT-0700 (Mountain Standard Time),23,5,11,1,2,7,7,12,3,14,2,9,2,5,21,17,5,6,6,14,31,20,28,9,33,29,1,24,7,6,6,3,32,39,45,45,33,12,18,47
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,7,14,12,16,17,10,17,20,9,21,3,5,25,23,5,8,25,9,21,6,25,8,15,24,22,12,33,25,35,4,27,21,13,24,5,37,37,14,4
Thu Feb 13 2020 00:00:00 GMT-0700 (Mountain Standard Time),27,1,0,9,7,3,13,9,5,6,15,2,10,8,14,14,20,12,31,15,14,34,22,9,5,12,4,5,30,2,2,28,44,9,19,17,15,46,10,8
Fri Feb 14 2020 00:00:00 GMT-0700 (Mountain Standard Time),29,4,5,4,7,6,16,10,15,21,19,10,8,7,11,29,28,6,14,11,23,1,3,8,31,3,7,37,19,4,18,32,9,5,44,32,28,8,26,26
Sat Feb 15 2020 00:00:00 GMT-0700 (Mountain Standard Time),31,14,3,6,2,13,18,12,1,13,23,12,3,2,10,2,2,25,1,20,11,5,0,21,10,12,22,23,15,0,4,19,37,1,23,44,1,17,52,4
Sun Feb 16 2020 00:00:00 GMT-0700 (Mountain Standard Time),33,15,12,12,6,6,11,12,3,17,18,12,3,26,4,16,24,3,3,2,29,8,23,37,36,15,26,36,8,34,46,43,44,40,1,34,8,5,0,14
Mon Feb 17 2020 00:00:00 GMT-0700 (Mountain Standard Time),35,12,2,11,21,13,23,19,25,8,17,28,5,4,7,6,17,2,4,16,7,18,28,18,12,9,6,32,38,1,44,35,12,31,35,5,7,33,22,41
Tue Feb 18 2020 00:00:00 GMT-0700 (Mountain Standard Time),37,3,7,9,14,4,24,17,15,1,16,0,25,26,18,3,7,16,1,22,34,39,40,2,23,19,1,6,15,9,33,35,18,8,33,38,8,13,37,37
Wed Feb 19 2020 00:00:00 GMT-0700 (Mountain Standard Time),39,10,7,9,5,2,21,9,5,13,16,27,27,20,7,9,30,7,21,17,11,35,0,31,38,39,6,22,43,5,30,15,38,23,49,43,25,43,5,45

Données de démarrage de la feuille de tableau de bord (csv) :

    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
HDR1,HDR2,HDR3,HDR4,HDR5,HDR6,HDR7,HDR8,HDR9,HDR10,HDR11,HDR12,HDR13,HDR14,HDR15,HDR16,HDR17,HDR18,HDR19,HDR20,HDR21,HDR22,HDR23,HDR24,HDR25,HDR26,HDR27,HDR28,HDR29,HDR30,HDR31,HDR32,HDR33,HDR34,HDR35,HDR36,HDR37,HDR38,HDR39,HDR40,HDR41
Fri Jan 31 2020 00:00:00 GMT-0700 (Mountain Standard Time),2,0,0,0,4,3,0,5,1,9,7,9,0,8,10,12,0,8,9,7,0,10,3,5,1,24,25,0,20,6,27,26,8,0,31,34,26,3,12,29
Sat Feb 01 2020 00:00:00 GMT-0700 (Mountain Standard Time),4,2,3,3,0,5,0,5,6,0,7,6,12,0,3,14,5,3,17,11,2,15,2,23,7,9,13,3,2,20,4,1,24,2,31,5,33,27,29,30
Sun Feb 02 2020 00:00:00 GMT-0700 (Mountain Standard Time),6,1,1,3,0,0,4,4,0,6,9,6,12,14,12,4,4,3,5,18,7,20,8,6,13,27,22,21,4,12,3,8,8,7,19,18,15,8,29,3
Mon Feb 03 2020 00:00:00 GMT-0700 (Mountain Standard Time),8,2,1,3,3,8,3,6,2,7,5,8,11,7,15,14,11,19,20,3,19,13,20,1,16,21,10,10,5,15,22,14,31,11,25,24,15,23,11,17
Tue Feb 04 2020 00:00:00 GMT-0700 (Mountain Standard Time),10,2,5,5,4,6,3,11,6,2,0,13,13,10,13,4,5,1,8,5,13,3,25,13,9,17,30,21,27,10,6,15,3,2,29,19,14,16,13,38
Wed Feb 05 2020 00:00:00 GMT-0700 (Mountain Standard Time),12,1,4,2,8,4,5,3,6,7,8,14,17,18,9,13,21,8,10,5,17,2,7,12,14,15,29,5,17,10,16,8,8,6,22,20,23,8,41,35
Thu Feb 06 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,2,8,9,5,4,3,6,7,12,9,9,1,7,8,15,14,21,2,9,9,11,23,19,3,0,27,27,27,4,36,13,9,23,11,38,23,17,5,40
Fri Feb 07 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,3,2,0,1,10,0,2,15,4,17,5,11,6,18,3,4,9,20,12,2,11,25,19,16,12,26,0,21,33,30,17,10,16,1,39,15,6,13,5
Sat Feb 08 2020 00:00:00 GMT-0700 (Mountain Standard Time),18,6,7,6,8,1,5,11,13,2,13,3,1,5,21,9,24,20,14,19,5,4,0,23,27,5,13,7,17,10,9,28,24,1,29,28,42,18,28,14
Sun Feb 09 2020 00:00:00 GMT-0700 (Mountain Standard Time),20,8,2,0,2,7,2,7,2,14,17,2,2,2,7,2,18,1,14,7,4,6,25,4,10,17,34,36,2,19,13,27,22,23,36,3,16,42,7,16
Mon Feb 10 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,4,1,8,14,0,7,15,5,9,18,8,1,5,8,3,15,10,2,10,16,18,17,11,0,7,19,1,33,12,38,20,9,9,33,5,4,43,3,27
Tue Feb 11 2020 00:00:00 GMT-0700 (Mountain Standard Time),24,7,6,12,7,14,5,1,14,1,14,8,2,15,0,25,2,0,2,25,6,24,18,26,8,10,25,21,33,10,27,15,33,30,36,39,38,15,46,9
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,9,0,5,1,2,17,5,18,16,18,17,8,17,0,12,11,4,5,27,5,9,24,21,22,19,3,32,24,12,9,33,19,5,5,1,6,48,43,50
Thu Feb 13 2020 00:00:00 GMT-0700 (Mountain Standard Time),28,5,13,15,13,4,5,1,14,0,17,6,18,17,19,4,6,22,21,30,15,6,11,34,10,32,19,20,29,16,32,42,38,26,37,19,20,4,38,25
Fri Feb 14 2020 00:00:00 GMT-0700 (Mountain Standard Time),30,5,12,13,1,1,7,13,19,13,3,17,4,16,15,14,6,11,12,11,0,11,33,28,4,13,1,2,22,4,40,37,24,27,41,19,22,19,8,24
Sat Feb 15 2020 00:00:00 GMT-0700 (Mountain Standard Time),32,15,12,10,5,15,8,21,6,2,0,9,8,0,28,21,26,13,9,30,3,17,6,8,14,28,36,28,30,41,26,46,36,22,13,39,5,10,21,12
Sun Feb 16 2020 00:00:00 GMT-0700 (Mountain Standard Time),34,10,6,3,1,17,18,11,19,8,9,7,20,20,18,27,21,30,25,12,10,25,14,37,20,32,15,10,23,5,3,0,48,13,26,14,6,39,34,27
Mon Feb 17 2020 00:00:00 GMT-0700 (Mountain Standard Time),36,15,7,19,5,15,17,20,4,19,22,15,24,19,29,3,21,15,12,23,14,11,32,14,10,39,31,24,45,37,29,3,15,22,44,52,52,38,7,47
Tue Feb 18 2020 00:00:00 GMT-0700 (Mountain Standard Time),38,3,0,21,18,19,21,2,1,10,6,15,5,7,29,12,16,28,31,8,5,30,36,1,35,28,8,21,1,9,33,32,24,22,34,51,25,12,36,9
Wed Feb 19 2020 00:00:00 GMT-0700 (Mountain Standard Time),40,6,13,3,19,0,18,8,10,25,6,19,27,8,7,15,35,32,2,25,21,3,25,38,20,12,31,20,6,4,5,49,29,37,50,22,51,21,1,13

Données du tableau de bord après l'exécution du script (csv) :

    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
HDR1,HDR2,HDR3,HDR4,HDR5,HDR6,HDR7,HDR8,HDR9,HDR10,HDR11,HDR12,HDR13,HDR14,HDR15,HDR16,HDR17,HDR18,HDR19,HDR20,HDR21,HDR22,HDR23,HDR24,HDR25,HDR26,HDR27,HDR28,HDR29,HDR30,HDR31,HDR32,HDR33,HDR34,HDR35,HDR36,HDR37,HDR38,HDR39,HDR40,HDR41
Fri Jan 31 2020 00:00:00 GMT-0700 (Mountain Standard Time),2,0,0,0,4,3,0,5,1,9,7,9,0,8,10,12,0,8,9,7,0,10,3,5,1,24,25,0,20,6,27,26,8,0,31,34,26,3,12,29
Sat Feb 01 2020 00:00:00 GMT-0700 (Mountain Standard Time),4,2,3,3,0,5,0,5,6,0,7,6,12,0,3,14,5,3,17,11,2,15,2,23,7,9,13,3,2,20,4,1,24,2,31,5,33,27,29,30
Sun Feb 02 2020 00:00:00 GMT-0700 (Mountain Standard Time),6,1,1,3,0,0,4,4,0,6,9,6,12,14,12,4,4,3,5,18,7,20,8,6,13,27,22,21,4,12,3,8,8,7,19,18,15,8,29,3
Mon Feb 03 2020 00:00:00 GMT-0700 (Mountain Standard Time),8,2,1,3,3,8,3,6,2,7,5,8,11,7,15,14,11,19,20,3,19,13,20,1,16,21,10,10,5,15,22,14,31,11,25,24,15,23,11,17
Tue Feb 04 2020 00:00:00 GMT-0700 (Mountain Standard Time),10,2,5,5,4,6,3,11,6,2,0,13,13,10,13,4,5,1,8,5,13,3,25,13,9,17,30,21,27,10,6,15,3,2,29,19,14,16,13,38
Wed Feb 05 2020 00:00:00 GMT-0700 (Mountain Standard Time),12,1,4,2,8,4,5,3,6,7,8,14,17,18,9,13,21,8,10,5,17,2,7,12,14,15,29,5,17,10,16,8,8,6,22,20,23,8,41,35
Mon Feb 10 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,9,8,2,14,14,1,11,4,0,2,12,13,13,2,12,23,8,28,14,1,23,18,33,33,7,17,12,16,21,24,5,20,12,29,39,3,28,28,26
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,7,14,12,16,17,10,17,20,9,21,3,5,25,23,5,8,25,9,21,6,25,8,15,24,22,12,33,25,35,4,27,21,13,24,5,37,37,14,4
Sat Feb 08 2020 00:00:00 GMT-0700 (Mountain Standard Time),18,6,7,6,8,1,5,11,13,2,13,3,1,5,21,9,24,20,14,19,5,4,0,23,27,5,13,7,17,10,9,28,24,1,29,28,42,18,28,14
Sun Feb 09 2020 00:00:00 GMT-0700 (Mountain Standard Time),20,8,2,0,2,7,2,7,2,14,17,2,2,2,7,2,18,1,14,7,4,6,25,4,10,17,34,36,2,19,13,27,22,23,36,3,16,42,7,16
Mon Feb 10 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,9,8,2,14,14,1,11,4,0,2,12,13,13,2,12,23,8,28,14,1,23,18,33,33,7,17,12,16,21,24,5,20,12,29,39,3,28,28,26
Tue Feb 11 2020 00:00:00 GMT-0700 (Mountain Standard Time),24,7,6,12,7,14,5,1,14,1,14,8,2,15,0,25,2,0,2,25,6,24,18,26,8,10,25,21,33,10,27,15,33,30,36,39,38,15,46,9
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,7,14,12,16,17,10,17,20,9,21,3,5,25,23,5,8,25,9,21,6,25,8,15,24,22,12,33,25,35,4,27,21,13,24,5,37,37,14,4
Thu Feb 13 2020 00:00:00 GMT-0700 (Mountain Standard Time),28,5,13,15,13,4,5,1,14,0,17,6,18,17,19,4,6,22,21,30,15,6,11,34,10,32,19,20,29,16,32,42,38,26,37,19,20,4,38,25
Fri Feb 14 2020 00:00:00 GMT-0700 (Mountain Standard Time),30,5,12,13,1,1,7,13,19,13,3,17,4,16,15,14,6,11,12,11,0,11,33,28,4,13,1,2,22,4,40,37,24,27,41,19,22,19,8,24
Sat Feb 15 2020 00:00:00 GMT-0700 (Mountain Standard Time),32,15,12,10,5,15,8,21,6,2,0,9,8,0,28,21,26,13,9,30,3,17,6,8,14,28,36,28,30,41,26,46,36,22,13,39,5,10,21,12
Sun Feb 16 2020 00:00:00 GMT-0700 (Mountain Standard Time),34,10,6,3,1,17,18,11,19,8,9,7,20,20,18,27,21,30,25,12,10,25,14,37,20,32,15,10,23,5,3,0,48,13,26,14,6,39,34,27
Mon Feb 17 2020 00:00:00 GMT-0700 (Mountain Standard Time),36,15,7,19,5,15,17,20,4,19,22,15,24,19,29,3,21,15,12,23,14,11,32,14,10,39,31,24,45,37,29,3,15,22,44,52,52,38,7,47
Tue Feb 18 2020 00:00:00 GMT-0700 (Mountain Standard Time),38,3,0,21,18,19,21,2,1,10,6,15,5,7,29,12,16,28,31,8,5,30,36,1,35,28,8,21,1,9,33,32,24,22,34,51,25,12,36,9
Wed Feb 19 2020 00:00:00 GMT-0700 (Mountain Standard Time),40,6,13,3,19,0,18,8,10,25,6,19,27,8,7,15,35,32,2,25,21,3,25,38,20,12,31,20,6,4,5,49,29,37,50,22,51,21,1,13

Image du tableau de bord final :

enter image description here

0
MetaMan 10 févr. 2020 à 20:33