Ben
Ben

Reputation: 1233

Newline for Google Calendar Api?

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!

Upvotes: 7

Views: 7460

Answers (9)

Santukon
Santukon

Reputation: 1

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();

Upvotes: 0

Florin Relea
Florin Relea

Reputation: 513

The following chars worked for me: %0A

Upvotes: 2

marzetti
marzetti

Reputation: 449

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.)

Upvotes: 0

johnnygoodman
johnnygoodman

Reputation: 475

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.

Upvotes: 1

Trevor
Trevor

Reputation: 16116

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.

Upvotes: 0

Gude
Gude

Reputation: 305

Using the API V3, this worked for me:

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

Upvotes: 2

jdewit
jdewit

Reputation: 1031

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;
}

Upvotes: 1

Alain
Alain

Reputation: 6034

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.

Upvotes: 2

Mark S.
Mark S.

Reputation: 4017

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

Upvotes: -2

Related Questions