Absolute reference with UDF & filtering data in google sheets script

北城余情 提交于 2020-05-09 07:16:07

问题


Hi I am begineer to scripts, here is the code and googlesheet for reference

What i am getting what i want to achieve /** *@param quest1 Question of the note *@param quest1 Answer of the note *@customfunction*/ function DMNOTE(quest1,ans1,quest2,ans2,quest3,ans3,) { var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var result = quest1+'-'+ans1+','+quest2+'-'+ans2+','+quest3+'-'+ans3; return result; }

I want to achieve absolute reference for "quest" parameter and i want it to loop for rest of the coulmns till column where i enter the function.Also under "Formula Required" column i have put formla for reference thats how i want my UDF to work.

Follwing up i need to filter "Non-solicit agreement" and keep only "No" under it and Copy & Paste all colums highlited in blue to update tab.

function toFilter (){
// filter and retain "no" in non-solicit agreement
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Worksheet");
ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn());
var createfilter = SpreadsheetApp.newFilterCriteria().setHiddenValues("Yes").build();
ss.getFilter().setColumnFilterCriteria(8, createfilter);
}

Hope i make sense. Any help is appriciated


回答1:


To work with any number of arguments, you can have a function like below in your script

function DMNOTE(...arg){
   let result = ''
   for(let i=0;i<arg.length;i++){
     result += `${arg[i]}-${arg[i+1]},`
     i++;
   }
   return result.substring(0, result.length - 1); 
}

And then form your spreadsheet you can call as =DMNOTE(A$1,A2,B$1,B2,C$1,C2) or =DMNOTE(A$1,A2,B$1,B2,C$1,C2,D$1,D2) The function will process all the arguments being passed and returns the result.




回答2:


How to filter "non-solicit agreement" and take only data which has "no"

The code you provided comes already very close to what you desire, you just need to make the following adjustments:

  • setHiddenValues() expect an array, no a string, so change "Yes" to ["Yes"]
  • getFilter() only works to modify a filter that is present in the sheet already, in case there is none, it is better to delete the old filter and create a new one:
function toFilter(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Worksheet");
  ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn());
  var createfilter = SpreadsheetApp.newFilterCriteria().setHiddenValues(["Yes"]).build();
  if(ss.getFilter() != null) {
    ss.getFilter().remove();
  }
  ss.getDataRange().createFilter().setColumnFilterCriteria(8, createfilter);
}

How to Copy & Paste all columns highlited in blue to update tab

Build a function that

  • calls toFilter()
  • checks for all rows either they are hidden
  • copies the non- hidden rows into an array
  • pastes this array to the sheet Update
function copyIfNotHidden(){
  toFilter();
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet1 = spreadsheet.getSheetByName("Worksheet");
  var sheet2 = spreadsheet.getSheetByName("Update");
  var data = sheet1.getDataRange().getValues();
  var array = [];
  for (var i = 0; i < data.length; i++){
    if(sheet1.isRowHiddenByFilter(i+1)==false){
      array.push(data[i]);
    }
  }
  sheet2.getRange(sheet2.getLastRow()+1, 1, array.length, array[0].length).setValues(array);
}


来源:https://stackoverflow.com/questions/61226191/absolute-reference-with-udf-filtering-data-in-google-sheets-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!