单元格范围的条件连接

Zab*_*eri 3 google-sheets google-apps-script

我正在尝试设置一个有条件的多重连接,并且几乎就在那里。

原则是(如 SUMIF)沿着一行/列运行,如果该值符合条件,则从总和范围中获取相应的值并将它们相加。虽然这次我们将它们连接起来(字面意思是将它们加在一起!)这张图片显示了预期的结果和我的实际结果,这应该比我能解释的更好。 电子表格 我遇到的问题是,无论我输入什么作为最后一个参数,它仍然给出一个没有任何空格的逗号(输入“#”仍然给出一个逗号等)...

这是代码:

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?谢谢。

Chr*_*ick 10

我认为一个公式可以为您做到这一点:

=JOIN(",",FILTER(B1:D1,B2:D2="Y"))