0

I am trying to create a custom function in Google Sheets which will copy a range of values vertically a given number of times:

original

So that when applying repeat(A1:A4,3), it pastes vertically A1 3 times, A2 3times, A3 3 times and A4 3 times.

result

My attempt seems to work well, displaying the result correctly, but it gives an error which I don't understand.

function repeat(range,number){
   var result = [];
   const l = range.length;
   for (var i=0;i<len<i++)
      for(var j=0;j<number;j++)
         result.push(range[i])
      return result;
}

The error displayed by Apps Script is: "TypeError: Cannot read property 'length' of undefined", indicating line 3. Please feel free to guide me on how to use this and whether there are easier/better options, as I am a beginner in both Apps Script and JS.

Joe
  • 27
  • 7

1 Answers1

1

You say that you are using this as a custom function so your range is actually a 2 dimensional array of values not a Class Range. Look up Custom Functions in the documentation.

If you insert this code into a spread:

function repeat101(values,count) {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  Logger.log(JSON.stringify(values));
}

Like this =repeat101(A1:A4,3)

Where A1:A4 looks like this:

1
2
3
4

Then look at your executions log you get this:

Executions Log: Jul 9, 2021, 2:08:58 PM Info [[1],[2],[3],[4]]

If your familiar with with using the debugger you will immediately recognize that as a 2 d column array

function REPEAT101(values, count) {
  const sh = SpreadsheetApp.getActiveSheet();
  let oA = [];
  values.forEach(r => { for (var j = 0; j < count; j++) { oA.push([r[0]]) } });
  return oA;
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • I have already mentioned I am a beginner, so no, I am not familiar with using the debugger. – Joe Jul 09 '21 at 20:42
  • Would you be able to adapt the answer so that it adjusts the method I was trying? I am struggling to adapt the JS coded answer from here, which is closer to my knowledge, and it is working perfectly. https://stackoverflow.com/questions/54266412/repeat-range-of-items-multiple-times-in-google-sheets – Joe Jul 09 '21 at 20:47
  • Sorry when I began working with Google Apps Script I already new quite a bit about Javascript. Back then it was assumed that knowing Javascript was a prerequisite. – Cooper Jul 09 '21 at 20:48
  • The function you presented says info null for me and asked for permission, but it still does not return anything. In the answer presented before, the author is using range for the same thing as I am trying to. – Joe Jul 09 '21 at 20:51
  • You say in your question your trying to create a custom function. What do you mean by a custom function? – Cooper Jul 09 '21 at 20:53
  • Here's what the Google Apps Script says about custom functions : https://developers.google.com/apps-script/guides/sheets/functions – Cooper Jul 09 '21 at 20:54
  • My function is working in my spreadsheet just fine. – Cooper Jul 09 '21 at 20:57
  • The first function in my code was just to be able to get a look at what a range input in a custom function looks like. So I just ran it to view executions and see that it's a two dimensional array. – Cooper Jul 09 '21 at 21:01
  • Can you update the function to match the one provided in the answer I presented please? It is not working for me for some reason (no idea why, I don't doubt your method), but this talk is not useful and I know what Google Apps Script says about custom functions., that is the page where I started the work on this from. The fact that I don't know JS should not be an impediment. If I knew, I wouldn't have asked here in the first place. I don't need a high school lesson of doing my homework. Please provide alternatives so I can actually learn something. – Joe Jul 09 '21 at 21:05
  • The function you added via edit works , but it still says TypeError: Cannot read property 'forEach' of undefined. – Joe Jul 09 '21 at 21:13
  • So, I suppose that is some sort of thing with the debugger or something that I wish you would help with, rather than arguing with me about my lack of knowledge. However, I do appreciate the addition, although you should have said in the comment you added it, rather than saying first function. It took me a while to find out, and it's just making things harder for a beginner. – Joe Jul 09 '21 at 21:15
  • You need to define values by selecting a range when you insert the function into a cell in the spreadsheet. Here's what mine looks like: `=REPEAT(A1:A4,3)` – Cooper Jul 09 '21 at 21:16
  • Are you using V8 runtime? – Cooper Jul 09 '21 at 21:30
  • Thank you for taking the time to look further into it, I appreciate the support. Yes, V8 is enabled in the project settings automatically, and I am trying to switch it off, but it keeps getting back on saying something about a missing ; I believe I am not declaring the array properly, but I cannot tell why. – Joe Jul 09 '21 at 21:41
  • Exactly how are you typing the function into the cell? – Cooper Jul 09 '21 at 21:43
  • =REPEAT(B3:B6,5) The problem is not in the cell, because the values are pasted correctly. The issue is with the debugger, because it throws the undefined error for both my code and for your code, although both functions work correctly. However, I did not know this until running your code as well. – Joe Jul 09 '21 at 21:46
  • You probably have other functions with the same name. Try changing the name. – Cooper Jul 09 '21 at 21:52
  • I do not, I have changed the name for both my function and yours many times by now. I have found several sources mentioning that this is an issue with the semicolon, but I cannot get it to work, regardless of where I am adding or removing it. – Joe Jul 09 '21 at 21:55
  • It is doing the same thing for every call of an array for which I am using .command after – Joe Jul 09 '21 at 21:56
  • I don't know what to tell you I changed mine to `=REPEAT101(B3:B6,5) ` and it works just fine. This is the whole point of a [mcve] because if we can't reproduce it then we are not likely to be much help in resolving the issue. – Cooper Jul 09 '21 at 22:01
  • I mean array.length or array.forEach or array.typeof – Joe Jul 09 '21 at 22:13
  • There is no array.typeof that I know of – Cooper Jul 09 '21 at 22:18
  • What about the other two? I have read something here about it, but no matter how I edit it, it's throwing the same error https://stackoverflow.com/questions/62070657/typeerror-cannot-read-property-foreach-of-undefined-javascript/62070791 – Joe Jul 09 '21 at 22:20
  • Nevermind this, it is working regardless, thank you for the solution. Would you be able to help with this please? https://stackoverflow.com/questions/68323183/transpose-n-rows-to-one-column-in-google-sheets-script-editor – Joe Jul 09 '21 at 22:29