Copy every row of Data from one sheet that has a g

2020-02-07 10:12发布

I'm working on a Google Spreadsheet the will use a Script to copy every row of Data from one sheet that has "Done In The Past 30 Days" in column K to another sheet.

By "sheet" I mean every tab. Source tab is "Arley" destination tab is currently "ArleysSandbox" Every row has ll columns, A-K. The entire row should be carried over. The number of total rows in the sheet will increase over time. Every time I run the Script, I want it to clear all existing data on "ArleysSandbox" first. Column K, where it says "Done In Past 30 Days" is a formula. I will eventually want to pull from multiple sheets and compile on the Sandbox sheet.

I was using a post from the old forum as a starting point but got stuck due to my lack of knowledge. http://productforums.google.com/forum/#!msg/apps-script/FNGzVkEqZh4/x8WCYtjvpI8J Thanks in advance for any suggestions.

2条回答
地球回转人心会变
2楼-- · 2020-02-07 10:32
//Find current filter and remove     
var currentFilter = master_sheet.getFilter();
      if ( currentFilter !== null ) {
        master_sheet.getFilter().remove();
      } 
     var master_lr=master_sheet.getLastRow();
     var master_lc=master_sheet.getLastColumn();


//Apply filter here
      master_sheet.getRange(1,1,master_lr,master_lc).createFilter();
      var criteria = SpreadsheetApp.newFilterCriteria().setHiddenValues(['Reject','Approved']).build();
      master_sheet.getFilter().setColumnFilterCriteria(45, criteria);

      master_sheet.getFilter().getRange().copyTo(harmonize_approvers.getRange(1,1,master_lr-1,master_lc), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
查看更多
Bombasti
3楼-- · 2020-02-07 10:45

don't be offended but I think this forum is not a market place where you can get apps for free, it is supposed to bring answers to poeple that try to get results by themselves.

That said, here is a 'skeletton' that should put you on the right tracks, I tried to explain with the comments what was happening. (didn't test it, maybe it needs debugging)

function copy2(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = SpreadsheetApp.setActiveSheet(ss.getSheets()[0]); selects the first sheet in your spreadsheet
  var data=sh.getDataRange().getValues();// data is a 2D array, index0 = col A
  // so column k is index 10
  var target=new Array();// this is a new array to collect data
  for(n=0;n<data.length;++n){ // iterate in the array, row by row
    if (data[n][10]=="Done In The Past 30 Days"){ ;// if condition is true copy the whole row to target
    taget.push(data[n]);// copy the whole row
    }
    }
    if(target.length>0){// if there is something to copy
  var sh2=SpreadsheetApp.setActiveSheet(ss.getSheets()[1]); //second sheet of your spreadsheet
  sh2.getRange(1,1,target.length,target[0].length).setValues();// paste the selected values in the 2cond sheet in one batch write
  }
} 
查看更多
登录 后发表回答