Javascript column help: Recurring classes

Hello, I am trying to setup a scheduling page where when a start date, recurrence pattern (1 for every day, 7 for weekly), and number of sessions (i.e 3) is provided, the remaining two dates are returned as an array.

In the data table, I have the following

I then want to use the Javascript column, and this is the setup:

The code (mind you, I am a total n00b) is here:

function calculateSchedule() {
    const startDate = new Date(p1);
    const recurrenceDays = parseInt(p2, 10);
    const numSessions = parseInt(p3, 10);
    let schedule = [];

    for (let i = 1; i < numSessions; i++) {
        let nextSessionDate = new Date(startDate.getTime() + recurrenceDays * 86400000 * i);
        schedule.push(nextSessionDate.toISOString());
    }

    return schedule;
}

const output = calculateSchedule();
output;

Glide Docs has info on the experimental column but nothing for the javascript column. In testing, I noticed console.log does not provide an output in the javascript column (duh); but it makes me think if there are other limitations within the javascript column? (Besides limited parameters.)

Any help would be greatly appreciated!

A few things.

  • You are not passing your parameters as p1, p2, or p3, so your parameters aren’t being used in your code.
  • You are not calling the function in your code, so it’s not even going to run.
  • You can’t return arrays from a javascript column. You will have to convert it to a comma delimited string first, return the string, then use a Split Text column to convert it to an array.

For starters, add a function call like this outside of the function.
return generateSessionSchedule(p1, p2, p3);

This will solve the first two points above.

3 Likes

Jeff, thank you for the time, and I am a partial idiot, I pasted the wrong code.

OK, you still need a return on ‘output’, but you don’t even need to define an output variable. Just do a return on the function call.

You still need to convert your array to a string.
Replace this:
return schedule;
With this
return schedule.join(',');

2 Likes

Lovely! Absolutely lovely! Thank you for your time.

The working code:

function calculateSchedule() {
    const startDate = new Date(p1);
    const recurrenceDays = parseInt(p2, 10);
    const numSessions = parseInt(p3, 10);
    let schedule = [];

    for (let i = 0; i < numSessions; i++) {
        let nextSessionDate = new Date(startDate.getTime() + recurrenceDays * 86400000 * i);
        schedule.push(nextSessionDate.toISOString());
    }

  return schedule.join(',');
}

return calculateSchedule();
output;

1 Like

Looks good!

You can get rid of output;. It’s not serving a purpose and is probably causing an unseen error because its an undefined variable.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.