1

Pls help me to understand what happening. I have code

    function splitWords() {
  var list = SpreadsheetApp.getActiveSheet();
  var list_Key = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Keys');
  if (list.getName() !== list_Key.getName()) {
    SpreadsheetApp.getUi().alert('Для работы функции перейдите на страницу Keys');
  } else {
    var array = list.getRange('C2:C').getValues();
    var t_array = array.filter(String).lenght;
    SpreadsheetApp.getUi().alert(array);
    var array1 = array.reduce(function (s, c) {
      var key = c[0];
      var keys = key.split(" ");
      for (var i in keys) {
        if (!s.includes(keys[i])) {                                       
          s.push(keys[i])
        }
      }
      return s;
    }, []);
     SpreadsheetApp.getUi().alert(array1);

    SpreadsheetApp.getActive().getActiveSheet().getRange(2,8,array1.length,array1[0].length).setValues(array1);
  }

}

When code comes to setValues() i have error. The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues

  • 1
    As per the [docs](https://developers.google.com/apps-script/reference/spreadsheet/range#setvaluesvalues), `The size of the two-dimensional array must match the size of the range.` - could that be the problem? – Tigger Sep 18 '20 at 23:36
  • Does this answer your question? https://stackoverflow.com/questions/63720612 – TheMaster Sep 19 '20 at 05:05

1 Answers1

1

In your script, array1 is 1 dimensional array. But it is required to be 2 dimensional array for serValues. I think that this is the reason of your issue. When you want to put the values of array1 to the range from "H2" to the row direction, it is required to include each element of array1 in an array. So how about the following modification?

From:

s.push(keys[i])

To:

s.push([keys[i]]);

Note:

  • you want to put the values of array1 to the range from "H2" to the column direction, how about the following modification?

    • From:

        SpreadsheetApp.getActive().getActiveSheet().getRange(2,8,array1.length,array1[0].length).setValues(array1);
      
    • To:

        array1 = [array1];
        SpreadsheetApp.getActive().getActiveSheet().getRange(2,8,array1.length,array1[0].length).setValues(array1);
      

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • But when I try to push like this ````s.push([keys[I]]);```` my function doesn't work correctly ((( – Alexander Vaschuk Sep 19 '20 at 00:04
  • @Alexander Vaschuk Thank you for replying. I apologize for the inconvenience. From `my function doesn't work correctly`, unfortunately, I cannot understand about it. This is due to my poor English skill. I deeply apologize for this. Can I ask you about the detail of your current issue? By this, I would like to think of it. By the way, when you provide the sample Spreadsheet and your current script for replicating your issue, I think that it will help to think of the issue. How about this? – Tanaike Sep 19 '20 at 00:10
  • @Alexander Vaschuk By the way, in your script, `SpreadsheetApp.getUi().alert()` is used. In this case, when the dialog is not closed, the next script is not run. Please be careful this. – Tanaike Sep 19 '20 at 00:15