Hi,
I had a slightly different need, wanted to update my data in a google spreadsheet and have modx retrieve those data and put them in placeholders that I place in my template.
based on
https://gist.github.com/pamelafox/770584
So here is the snippet , called it GoogleSpreadsheet :
<?php
// Google spreadsheet must be published
// Looks like:
// id-fr id-en (name) knowledge experience prospective
72 112 wordpress 70 75 85
81 113 css 50 15 30
$myGoogleSpreadsheetKey = $modx->getOption('myGoogleSpreadsheetKey', $scriptProperties, '');
$gid = $modx->getOption('gid', $scriptProperties, '0');
$cultureKey = $modx->getOption('cultureKey', $scriptProperties, 'fr');
$gdocRow = $modx->getOption('gdocRow', $scriptProperties, '1');
$gdocColumns = $modx->getOption('gdocColumns', $scriptProperties, '');
$columns = explode(',',$gdocColumns);
$url = 'http://spreadsheets.google.com/feeds/list/' . $myGoogleSpreadsheetKey . '/'. $gid .'/public/values?alt=json';
$file= file_get_contents($url);
$json = json_decode($file);
$rows = $json->{'feed'}->{'entry'};
foreach($rows as $row) {
// define cultureKey : because I use 2 languages, when translating I need the data to load also for my translated language
if($cultureKey =='fr') $id = $row->{'gsx$id-fr'}->{'$t'};
if($cultureKey =='en') $id = $row->{'gsx$id-en'}->{'$t'};
if($id == $gdocRow){
foreach($columns as $idx=>$column) {
$modx->setPlaceholder('column.name.'.($idx+1),$column);
$modx->setPlaceholder('column.value.'.($idx+1),$row->{'gsx$'.$column}->{'$t'});
}
break;
}
}
and then use it like this
[[GoogleSpreadsheet? &key=`[[++myGoogleSpreadsheetKey]]` &gid=`[[++myGoogleSpreadsheetGid.myServices]]` &cultureKey=`[[++cultureKey]]` &gdocRow=`[[+id]]` &gdocColumns=`knowledge,experience,prospective`]]
<div class="skillbar clearfix " data-percent="[[+column.value.1]]%">
<div class="skillbar-title"><span>[[%[[+column.name.1]]? &namespace=`my_site` &topic=`skillbars`]]</span></div>
<div class="skillbar-bar"></div>
<div class="skill-bar-percent">[[+column.value.1]]%</div>
</div>
<div class="skillbar clearfix " data-percent="[[+column.value.2]]%">
<div class="skillbar-title"><span>[[%[[+column.name.2]]? &namespace=`my_site` &topic=`skillbars`]]</span></div>
<div class="skillbar-bar"></div>
<div class="skill-bar-percent">[[+column.value.2]]%</div>
</div>
<div class="skillbar clearfix " data-percent="[[+column.value.3]]%">
<div class="skillbar-title"><span>[[%[[+column.name.3]]? &namespace=`my_site` &topic=`skillbars`]]</span></div>
<div class="skillbar-bar"></div>
<div class="skill-bar-percent">+[[+column.value.3]]%</div>
</div>
Notice I used context settings to define my spreadsheet key and sheet gid (which is the sheet index, you can get it when editing your google doc)
The placeholders ares generated depending of the content of gdocColumnswhich is separated by commas.
The placeholders have index in the same order as the list of gdocColumns, it can output the name and the value
The id-fr and id-en are the id of the resources which will output the data.
[[+id]] needs to be replaced by your ressource id. As this code is a chunk, I call it in another chunk like this :
[[$mySkillbars.myServices? &id=`[[*id]]`]]
and inside this chunk I have the chunk I posted.
If you use it directly in template, you use [[*id]] for &gdocRow
[ed. note: robotpapier last edited this post 12 years, 4 months ago.]