-1

need help, i try to read a cell in this fonction. I've tried 20 differents type of thing, i always have this error message when i run the focntion on my spreadsheet :

Message details :

Cannot convert function getData() { Sheet = SS.getSheetByName("Technic"); Range = Sheet.getRange(4, 2); Data = Range.getValue(); } to (class).

What wrong ?

function hideMyColumns()
{
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getActiveSheet();
  var vl = sh.getRange('A30').getValue();
  var cols1 = [ function getData() {
          Sheet = SS.getSheetByName('Technic');
          Range = Sheet.getRange(4, 2); 
          Data = Range.getValue() }];
  var cols2 = [4, 5, 7, 8, 10, 11,]
  switch (vl)
  {
    case 1:
      for (var i = 0; i < cols1.length; i++)
      {
        sh.hideColumns(cols1[i]);
      }
      break;
    case 2:
      for (var i = 0; i < cols2.length; i++)
      {
        sh.hideColumns(cols2[i]);
      }
  }
}

évolution of the situation :

ok, now the fonction is like this :

function hideMyColumns()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var vl=sh.getRange('A30').getValue();
var cols1=[ function() { 
Sheet = ss.getSheetByName('Technic');
Range = Sheet.getRange(2,4);
Data = Range.getValue();
return Data;
}()]; 
var cols2=[4,5,7,8,10,11,12,]
switch(vl)
{
case 1:
  for(var i=0;i<cols1.length;i++)
  {
    sh.hideColumns(cols1[i]);
  }
  break;
  case 2:
  for(var i=0;i<cols2.length;i++)
  {
    sh.hideColumns(cols2[i]);
  }
}

}

the context :

the spreadsheet have 2 sheet : "Main" and "Technic"

"Main" can have some columns hide by this fonction. In the sheet "Technic" you find a cell that contains a formula that returns:"2,3,4..." i try to use this formule like a variable in the fonction "hideMyColumns".

Under this i have another fonction more simple : unhideCols() of course. (work well!)

C.Jay
  • 15
  • 5
  • 1
    The error message is quite clear - Google Apps Script doesn't support defining a function as an array element in the manner that you attempt. Further, that function doesn't even do anything - if it were to execute, it would only read a value and then discard that value -- effectively the equivalent of not even having been written. – tehhowch May 04 '18 at 20:20

1 Answers1

0

You want to give a value to cols1 using the function expressions. If my understanding is correct, how about this modification?

Modification points :

  • In your situation, when var cols1 = [function getData(){...}] is used as the function expressions, please modify to var cols1 = [function getData(){...}()] or var cols1 = [function(){...}()].
  • SS of SS.getSheetByName('Technic') is not declared.
    • When getData() is run, the error occurs here.
    • At the modified script, SS was modified to ss.
  • There are no returned values in getData().
    • By this, cols1 always becomes [].

Modified script :

var cols1 = [ function() { // Modified
  Sheet = ss.getSheetByName('Technic'); // Modified
  Range = Sheet.getRange(4, 2);
  Data = Range.getValue();
  return Data; // Added
}()]; // Modified

Note :

  • In this modified script, only var cols1 was modified.
  • If you want to use SS, please declare it.
  • If you want to return other values, please modify it.
  • From your script, it seems that cols1 has only one element. So you can modify from sh.hideColumns(cols1[i]); to sh.hideColumns(cols1[0]); without for loop. Or when it is declared as var cols1 = function(){...}(), you can modify to sh.hideColumns(cols1); without for loop.

References :

If I misunderstand your question, I'm sorry.

Edit :

If you want to use the values of column D as ranges, please try this modified script.

function hideMyColumns()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var vl=sh.getRange('A30').getValue();
var cols1=function() { // Modified
Sheet = ss.getSheetByName('Technic');
Range = Sheet.getRange(3,4); // getRange(3,4) is D3, Modified
Data = Range.getValue();
return Data.split(","); // Modified
}();
var cols2=[4,5,7,8,10,11,12,]
switch(vl)
{
case 1:
  for(var i=0;i<cols1.length;i++)
  {
    sh.hideColumns(cols1[i]);
  }
  break;
  case 2:
  for(var i=0;i<cols2.length;i++)
  {
    sh.hideColumns(cols2[i]);
  }
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • we are getting closer ! – C.Jay May 05 '18 at 06:44
  • we are getting closer ! the error message have change and now the fonction read the content of the cell. But still doesn't work. a new error message appears. :Cannot convert 6,7,8,10,11,12,13,14 to (class). Sorry to not be so clear, i just discover this part of google spreadsheet and its a brand new world for me. – C.Jay May 05 '18 at 06:52
  • @C.Jay Can you show us your latest script? If you can do, please add it to your question. In my environment, when your script is modified by my answer, no error occurs. – Tanaike May 05 '18 at 06:52
  • i just post it in another answer because the comment was to short. – C.Jay May 05 '18 at 07:12
  • @C.Jay You posted a question as an answer. Please add it to your question using the edit button, and remove the answer. If that is your answer, I'm sorry. – Tanaike May 05 '18 at 07:15
  • @C.Jay In my environment, no error occurs. Your situation cannot be reproduced. So can you share a sample spreadsheet with your error? – Tanaike May 05 '18 at 07:20
  • oups ok, i do that – C.Jay May 05 '18 at 07:37
  • @C.Jay Thank you for your response. In my environment, no error occurs. Your situation cannot be reproduced. So can you share a sample spreadsheet with your error? – Tanaike May 05 '18 at 07:40
  • ok : https://docs.google.com/spreadsheets/d/1y_FsQfd5aI8wdvS4Ng3E9SXcgZJH2ayiVTYkOdljmLA/edit?usp=sharing It's a copy so no risk – C.Jay May 05 '18 at 07:44
  • @C.Jay It seems that ``Sheet.getRange(2,4)`` which is "D2" on "Technic" has no value. – Tanaike May 05 '18 at 07:50
  • Yea, i see that and change it,(i was trying different thing so i try with another cell...) but if you put Sheet.getRange(4,3) it does not work either – C.Jay May 05 '18 at 07:58
  • any idea with this modification ? – C.Jay May 05 '18 at 08:45
  • @C.Jay It seems that Sheet.getRange(4,3) which is "C4" on "Technic" is "Iti Lièges". This is a string. Please read [this document](https://developers.google.com/apps-script/reference/spreadsheet/sheet#hidecolumnscolumnindex) for ``hideColumns(columnIndex)``. – Tanaike May 05 '18 at 08:45
  • @C.Jay You mean that you want to use the values of column D as ranges? – Tanaike May 05 '18 at 08:53
  • @C.Jay I updated my answer. If my understanding is correct, please confirm it. – Tanaike May 05 '18 at 08:58
  • @C.Jay I updated my answer. At the updated answer, the values of column D are used as the ranges. Please confirm it. – Tanaike May 05 '18 at 09:05
  • God !! yes, I can not believe it! it works ! Tanaike you are amazing! I tried it for so long. It's perfect. So, I understand that it is necessary to divide the contents of the cell with each comma, right? – C.Jay May 05 '18 at 09:17
  • @C.Jay I'm glad your issue was solved. Thank you, too. – Tanaike May 05 '18 at 11:48