Select and copy some values from spreadsheet comparing to another column on another sheet

后端 未结 1 421
情深已故
情深已故 2021-01-25 16:24

I have read these two links Google Script: Conditionally copy rows from one spreadsheet to another and https://stackoverflow.com/a/4809413/1526044 and after trying I am not able

相关标签:
1条回答
  • 2021-01-25 16:54

    I modified a similar script I answered recently that should do what you need. I didn't test but you can give it a try. There are some comments to explain the idea... it probably needs some debugging.

    EDIT : I played around with this and debugged it a bit more ;-) so it seems to work as needed now - here is the link to my test sheet

    function xxx(){ 
    /* we have an array 'keysheet'
        and an array datasheet , you get these with something like*/
            var keysheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getDataRange().getValues();
        // and
            var datasheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1].getDataRange().getValues();// if other columns, change index values in the arrays : 0=A, 1=B ...
        // the iteration could be like this :
                  for(i=0;i<datasheet.length;++i){ // don't miss any data
                    for(j=0;j<keysheet.length;++j){ // iterate through keysheet to find the keys
                      for(k=1;k<keysheet[0].length;++k){ // iterate in keysheet row to check every column except first one
                          var key = keysheet[j][k]
                          Logger.log(k+' '+key+"  "+datasheet[i].toString().match(key))
                     if(datasheet[i].toString().match(key) == key){// check whole rows to find if key is in data
                      var x = datasheet[i].shift();
                       datasheet[i].unshift(keysheet[j][0]) // if match found, replace first element in datasheet with fisrt element in keysheet
                         break  // if found break k loop maybe you should also break j loop ?
                           }
                     }
                       } //loop keysheet
                 } // loop datasheet
    
                /* now you have a modified datasheet array that can directly overwrite the old datasheet 
                using setValues() */
                var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];// assuming the datasheet sheet is sheet nr2
                Logger.log(datasheet)
               sh.getRange(1,1,datasheet.length,datasheet[0].length).setValues(datasheet);
                  }
            //
    
    0 讨论(0)
提交回复
热议问题