0

EDIT Thanks for reopening - I have redrafted the question to make it (hopefully) a bit sharper. /EDIT

I wrote an Apps Script function "remainingDigits" (see below). As input parameters, the function takes 0-10 digits in the form (n,n,..,n) where 0<=n<9.

The purpose of the function is to calculate which digits in the range 0-9 were NOT passed as input parameters e.g. if the inputs were 6, 7, 2 & 4 then the result should be 0, 1, 3, 5, 8 & 9.

The function treats the inputs as an array of values and on completion returns an array of values.

Using a very simple test harness (see below), if executed entirely within Apps Scripts, this works perfectly.

However, when called from a cell in Sheets, it exhibits what is (to me at least) rather bizarre behaviour, as follows:

Cell Values (for all tests) : A7=6, A8=7, A9=2, A10=4

Test 1

Calling cell function "=remainingDigits(A7, A8, A9, A10)"

Response in Sheets Error message

"TypeError: digits.indexOf is not a function (line 20)"

Test 2

Calling cell function "=remainingDigits({A7, A8, A9, A10})"

Response in Sheets Value of the calling cell is set to 0, and the value of the 9 cells beneath it are set to 1,2,3 .... 9

Test 3

Calling cell function "=remainingDigits(A7:A10)"

Response in Sheets Same as Test 1

Test 4

Calling cell function "=remainingDigits({A7:A10})"

Response in Sheets Same as Tests 3

All very strange !

Any tips for fixing this would be much appreciated :)

Here's the code:

function testRD() {
// A simple test harness for remainingDigits function

  let results = new Array;

  results = remainingDigits([0,1,2,3,4]);
  
  var a;

  for(a=0;a<=results.length-1;a++){
    Logger.log(results[a]);  
  }
}

function remainingDigits(digits){

  let remDigits = new Array;
  var d;

  for(d=0;d<=9;d++){
    if(digits.indexOf(d)!=-1){
    }
    else {
      remDigits.push(d);
    }
  }
  return remDigits;
}
  • Just for information, if I amend the function to return a string instead of an array (e.g. "013589") then the calling cell displays "0123456789" !!! – Chromasome Oct 15 '22 at 10:30
  • If the linked duplicate answers doesn't answer your question, [edit] your question to explain how the answers don't satisfy your question. Note that there's a proper [etiquette](https://meta.stackoverflow.com/questions/252252) to dispute a duplicate. If you're having trouble implementing a solution in the duplicate or don't understand a solution provided, ask a new specific question with the part you're having trouble with. – TheMaster Oct 15 '22 at 10:40
  • The duplicate shows the actual structure of the array you'll be receiving as `digits`, which is a 2D array. An easy way to fix your code without changing much is just flatten `digits` before doing any operation. – TheMaster Oct 15 '22 at 10:42
  • `passing values INTO a script` If you understand how the values are passed, you'll fix your problem. – TheMaster Oct 15 '22 at 10:59

0 Answers0