Home > Software engineering >  What's wrong with CalendarApp.createCalendar options?
What's wrong with CalendarApp.createCalendar options?

Time:08-16

I'm working on a script to create and share a calendar with Google Sheets, but it seems that I can't get everything I would like to, the options are not working. Here is the code:

function createCalendar() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Détail");
  SpreadsheetApp.setActiveSheet(sheet);
  
  const calName = sheet.getRange(3, 1).getValue();
  const year = sheet.getRange(3, 2).getValue();
  const color = sheet.getRange(3, 4).getBackground();
  const calendar = CalendarApp.createCalendar(
    `District ${nomCal}`, 
    {
      summary: `Here you'll find all the importants events in the ${district} for the year ${year}`,
      timeZone: "Europe/Paris",
      color: color
    }
  );
  
  console.log('created calendar "%s", with ID : "%s", description: "%s" and color: "%s".',
    calendar.getName(), calendar.getId(), calendar.getDescription(), calendar.getColor());

the calendar is created but the summary is empty

The log:

23:18:45 Infos Création du calendrier "Province PACA", avec ID : "[email protected]", description: "" et couleur: "#ff9900".

I checked the scopes, everything seems ok, I don't understand.

Can anyone help me?

Thanks

CodePudding user response:

I get a result using setDescription after creating the calendar:

function createCalendar() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("focus");
  SpreadsheetApp.setActiveSheet(sheet);
  
  const calName = sheet.getRange(3, 1).getValue();
  const year =  sheet.getRange(3, 2).getValue();
  const color = sheet.getRange(3, 4).getBackground();
  const description = sheet.getRange(5, 1).getValue();
  
  //Création du calendrier
  const calendar = CalendarApp.createCalendar(
    `${nomCal} district`, 
    {
      timeZone: "Europe/Paris",
      color: color
    }
  );

  calendrier.setDescription(`${nomCal} district calendar for the year ${annee}\n${description}`)

Thanks for your help!

CodePudding user response:

The comma operator evaluates all expressions and returns the right most. So, ('some text "%s"', year) just evaluates to year( which is probably empty, in this case).

Use template literals instead:

summary: `some text ${year}`

The color should be a specific color from CalendarApp.color:

color: CalendarApp.Color.BLUE

or a hexColor string retrieved from getBackground:

color: color //or just color( without :)

CodePudding user response:

There are some syntax issues with your code. Try fixing them like this:

function testCreateCalendar() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const calName = sheet.getRange('A4').getDisplayValue();
  const year = sheet.getRange('B4').getDisplayValue();
  const color = sheet.getRange('D4').getBackground();
  const calendrier = CalendarApp.createCalendar(
    `Calendar for ${calName}`,
    {
      summary: `here you will find all the importants events for the period ${year}`,
      timeZone: 'Europe/Paris',
      color: color,
    }
  );
  console.log(`Calendar created with ID ${calendrier.getId()}`);
}

See Template literals.

  • Related