I am trying to set up a conditional multi-concatenate and am almost there.
The principle is (like a SUMIF) you run along one row/column and if the value matches the condition you take the corresponding value from the sum range and add them together. Though this time we're concatenating them (literally adding them together!)
This picture shows the expected result and my actual result which should explain better than I can.
The issue I'm having is that no matter what I input as the last parameter it still gives a comma without any space (inputting "#" still gives a comma etc)...
Here's the code:
/**
* Concatenates a range of cells where the condition is met.
*
* @param {A4:A6} cRange The dynamic cell range to test.
* @param {"Condition"} cCondition The string that the cell should match.
* @param {A4:A6} pRange The dynamic cell range to concatenate.
* @param {", "} interspace A string to insert between each entry.
* @return The a concatenated range of cells
* @customfunction
*/
function conditionalMultiConcat(cRange, cCondition, pRange, interspace){
var ss = SpreadsheetApp.getActiveSpreadsheet(); //get the active workbook
var sheet = ss.getSheets()[0]; //get the active sheet
//var values = sheet.getRange(pRange).getValues(); //debug line - uncomment to see the values
var values = "" //set the return value to blank
for(var cc = 0; cc < pRange.length; ++cc) { //For each value in the array
if ((cc == 0 || cc == pRange.length - 1) && (cRange[cc] = cCondition)) { //if it's the first or last value AND it matches our condition
values = values + pRange[cc].toString();// concatenate without interspace
}
else if (cRange[cc] = cCondition){ //else if it matches our condition then concatenate with the interspace
values = values + interspace + pRange[cc].toString();// concatenate
}
}
return values;
}
What am I missing here? Thanks.
I think a formula can do this for you:
=JOIN(",",FILTER(B1:D1,B2:D2="Y"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With