#text.split() shenanigans

37 messages · Page 1 of 1 (latest)

clear wren
#

hi i am brand new
i'm writing a custom function for google sheets
i intended for text.split() to return an array
i dont understand what TypeError: Cannot read properties of undefined (reading 'split') means
it looks like it goes something like string.split(sep) -> array

function toMinutes(time) {
  const array = time.split(":");
  //return (array[0] * 60) + array[1];
}

//console.log(toMinutes("1:20"))
toMinutes("1:20")
#

i would prefer to do something like

const [hours, minutes] = time.split(":");

but i figured it probably wasnt helping my case to split off from w3schools's split example yet

#

suppose i could learn the basics first

clear wren
#

not cool

icy moss
#

The error means that time is undefined.

#

./run

function toMinutes(time) {
  const array = time.split(":");
  console.log(array);
}

toMinutes("1:20")
river karmaBOT
#

Here is your js(18.15.0) output @icy moss

[ '1', '20' ]
icy moss
#

There might be an issue with your editor, or you forgot to save at some point.

clear wren
#

it is the google sheets custom function editor

icy moss
#

Ah gotcha, yeah it is likely something specific to that editor. As you can see this code is valid in isolation

clear wren
#

can i specify that the argument will always be a string

#

i read something about javascript being a silly language that hates me and doesnt do types like that

icy moss
#

It doesn't seem to be getting the argument in the first place.

#

But no, you can not specify and argument type in JS

clear wren
#

silly google sheets

icy moss
#

Could you quickly screenshot the entire window?

clear wren
icy moss
#

So I am not familiar with app scripts, but could you try something?
Update the entire snippet to this and run it, share the output

function toMinutes(time) {
  console.log('toMinutes running');
}
clear wren
#

sure but hold on it's passing period

#

it runs

icy moss
#

Please delete every thing other than that function

#

Specifically line 15

clear wren
#

it also at least knows what the argument is

clear wren
clear wren
#

i did not expect it to

icy moss
#

Yeah seems like app scrips runs that function directly rather than looking for a function call like toMinutes("1:00"). Because of this I am not sure how to pass in arguments

clear wren
#

it makes sense that it would run though because the whole point is a custom google sheets function

icy moss
#

But if you want the data to be driven from the sheet you can still do that

foggy pebble
#

function toMinutes(time) {
if (typeof time == 'string') {
const [hours, minutes] = time.split(":");
return (parseInt(hours) * 60) + parseInt(minutes);
} else {
console.log('Invalid input');
return 0;
}
}

toMinutes("1:20");
check this code

icy moss
#

OPs original snippet works, this was more of an issue/confusion with google app scripts

foggy pebble
#

there are 2 issues in origin code.

#

First is === and Second is there isn't return in function.

clear wren
#

i have experience in some other languages

#

i opened up the editor and googled everything i needed to know