7

I'm trying to insert the description of an event in google calendar from my web application and i cant get \n or <br /> to be interpreted as a line break. How does the google calendar interpret newlines? Help would be appreciated!

Ben
  • 1,233
  • 2
  • 14
  • 17

9 Answers9

2

Using the API V3, this worked for me:

$full_description .= 'Evento organizado por: ' . $area_responsavel . "\n\n"; $full_description .= $mensagem;

Gude
  • 305
  • 2
  • 13
  • 2
    A single "\n" worked for me. It's important to use double quotes though! (single quotes won't work) – tiho Feb 17 '15 at 03:23
2

The following chars worked for me: %0A

Florin Relea
  • 513
  • 6
  • 15
2

Are you using a specific client library? If using the protocol, simply putting newline in the content element should work:

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
       xmlns:gCal="http://schemas.google.com/gCal/2005"
       xmlns:gd="http://schemas.google.com/g/2005">
  <title type="text">Event with new line</title>
  <content type="text">This is an event with one
two
three
and and four lines.</content>
<gd:when endTime="2011-12-23T10:00:00.000-07:00" 
         startTime="2011-12-23T08:00:00.000-07:00"/>
</entry>

If using a client library, using '\n' should work as well.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Alain
  • 6,044
  • 21
  • 27
1

Use this to create a line break in a Google Calendar event description:

\\n

Randell McGlynn wrote the correct answer in comments:

I'm sure something has changed in the past 3 years since this question was asked, but you can now do it. You just need to format your string for JSON. \n becomes \\n.

Laurel
  • 5,965
  • 14
  • 31
  • 57
johnnygoodman
  • 475
  • 6
  • 19
1

Heredocs is how I got it to work.

$content = <<<EOT
This 
is 
my 
content
EOT;

You can't loop inside of a heredoc but you can build it up like so

$content = '';
for ($vars as $var) {
$content .= <<<EOT 
$var

EOT;
}
jdewit
  • 966
  • 9
  • 10
0

You can use an intent to add the description using \n for the newline. This code

        Intent intentCal = new Intent(Intent.ACTION_INSERT)
                .setData(CalendarContract.Events.CONTENT_URI)
                .putExtra(CalendarContract.Events.ALL_DAY, true)
                .putExtra(CalendarContract.Events.TITLE, "title")
                .putExtra(CalendarContract.Events.DESCRIPTION,"abc\ndef");
        startActivity(intentCal);

produces the event description

abc
def

If you are using a string instead of the literal above, be sure the string doesn't already have the slashes escaped, ie. "abc\\ndef" - when you output the string (eg. using Log.d) it looks like "abc\ndef", but of course it is not the same. For example, if you get a selection from a webview using

webview.evaluateJavascript("(function(){return window.getSelection().toString()})()",
                        value->calendarString(value));

then the string sent to the calendarString function in value has escaped slashes, and produces the event description

"abc\ndef"

(complete with beginning and ending quotes.)

marzetti
  • 369
  • 4
  • 8
0

Found the way!

Simply create an array with each line content and then make an echo of the array imploded by br tag inside ob reading.

$eventDescriptionArray = [
 'line_1' => 'content',
 'line_2' => 'content',
 'line_3' => 'content'
];

ob_start();
echo implode( '<br/>', $eventDescriptionArray );
$eventDescription = ob_get_contents();
ob_clean();
Santukon
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 20 '22 at 20:46
0

I'm not sure how google calendar interprets new lines either but there seems to be 121 characters per line.

So say you wanted to put "Address:"/n into details of the google calendar.
Take 121 subtract the Character count of "Address:" and append 113 spaces to "Address:"

the following text should be on a new line.

Even easier if the text your sending is a PHP variable make the new line in php.

$description = 'Description:'.'\n';

Then google calendar will read it as a new line.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Trevor
  • 16,080
  • 9
  • 52
  • 83
-2

"/n" only works for iCal entries. Using the Google Calendar API, you cannot do newlines.

Mark S.
  • 3,849
  • 4
  • 20
  • 22
  • so its impossible to have a newline for event description...? – Ben Oct 20 '11 at 03:43
  • Through the Google API, yes. Through an iCal interface, you can use "\n" – Mark S. Oct 20 '11 at 04:09
  • 2
    I'm sure something has changed in the past 3 years since this question was asked, but you can now do it. You just need to format your string for JSON. "\n" becomes "\\n". Here is a useful C# function to do it: http://stackoverflow.com/questions/1242118/how-to-escape-json-string/21455488#21455488 – Vandel212 Mar 24 '14 at 18:32