Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsrhino

Google Script / js script not updating current value


This is the current code: last question --> how do i make every single outcome in one string. (visit log in screenshot)

var outcome should be like :

Nik | HUR-L2 | Laranite | 4564 + "\n" + Tobi | CRU-L1 | Quanti | 513 + "\n"

enter image description here


Solution

  • You can just grab all table date at once via getRange('your range').getValues() and then filter it this way:

    const table = [  // just as an example
        [1235, 1],
        [4564, 0],
        [452, 1],
        [513, 1]
    ]
    
    const values = table.filter(t => t[1] == 1).map(t => t[0])
    
    console.log(values); // [ 1235, 452, 513 ]

    For your case the code can be boiled down to this:

    function test() {
      const sSheet   = SpreadsheetApp.getActiveSpreadsheet();
      const srcSheet = sSheet.getSheetByName("Refinery");
      const table    = srcSheet.getRange("D:M").getValues(); // use "D2:M" if you have a header
      const values   = table.filter(t => t[9] == 1).map(t => t[0]);
    
      Logger.log(values); // [1235.0, 452.0, 513.0]
    }
    

    enter image description here

    Update

    If you want data from C, D, E you can do this:

    const table    = srcSheet.getRange("C:M").getValues(); // get a wider range
    
    const values_C = table.filter(t => t[10] == 1).map(t => t[0]);
    const values_D = table.filter(t => t[10] == 1).map(t => t[1]);
    const values_E = table.filter(t => t[10] == 1).map(t => t[2]);
    

    The table is a table from a given range ("C:M"), where table[0][0] is C, table[0][1] is D, etc.

    The filter() part removes from the table rows that don't have "1" in 10th column (table[0][10] is M).

    The map() part removes all cells but one from every row.

    Or (more efficient but less readable variant):

    const [values_C, values_D, values_E] = 
          table.filter(t => t[9] == 1).map(t => [t[0], t[1], t[2]]);
    

    This way you will get an array. To get a string you need one more step:

    const values_C_string = value_C.join(", ") // [a,b,c] --> "a, b, c"
    

    You can chain this step this way:

    const values_C = table.filter(t => t[10] == 1).map(t => t[0]).join(", ");