Jump to content

Fetching Rows and Columns from Google Sheet into Div


CosetteGirardot

Recommended Posts

http://stackoverflow.com/q/26809213/4080419

GOAL: To have multiple people edit a Google Spreadsheet and have all edits display on a webpage (not Google site) where my css takes over the formatting and display of the content coming from the Spreadsheet.

PROBLEM: I am brand new to PHP, as in I've been doing tutorials all week and this is my first time working with it. Likewise, I'm brand new to Google API's and don't fully understand how they work. I don't know what code to use or really how to modify it.

WHAT I'VE TRIED: Google API obviously has the code for how to fetch the data to put into a page, so I've copied this, though I don't know if I put it in correctly/Dreamweaver says "There is a syntax error on line 32. Code hinting may not work until you fix this error". Additionally, I've read that perhaps Google Fusion Tables might be better because I can download it as a CSV version and then have that be what used in my actual script (though I don't fully understand how/if that would continually update itelf?):

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Untitled Document</title><!--#include file="php-5.6.2.tar.bz2" --><?php include("php-5.6.2.tar.bz2"); ?><style type="text/css">#page {margin-left:auto;margin-right: auto;top: 15%;bottom: 15%;width: 1000px;}.menue {border-color:rgb(0,0,0);border-width:thick;height:300px;width: 300px;position: abosolute;margin-left: 10%;margin-right: 10%;}</style></head><body><div id="page"><div id="Skillet_Product_List_and_Price" class="menue"><?phpusing Google.GData.Client;using Google.GData.Spreadsheets;namespace MySpreadsheetIntegration{class Program{static void Main(string[] args){SpreadsheetsService service = new SpreadsheetsService("https://docs.google.com/a/emich.edu/spreadsheets/d/1OAFhMrayek9BKvO6ZI9EIPmCeY61oLL7-wfZOIg2xV8/edit#gid=724477237");// TODO: Authorize the service object for a specific user (see other sections)// Instantiate a SpreadsheetQuery object to retrieve spreadsheets.SpreadsheetQuery query = new SpreadsheetQuery();// Make a request to the API and get all spreadsheets.SpreadsheetFeed feed = service.Query(query);if (feed.Entries.Count == 0){// TODO: There were no spreadsheets, act accordingly.}// TODO: Choose a spreadsheet more intelligently based on your// app's needs.SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];Console.WriteLine(spreadsheet.Title.Text);// Get the first worksheet of the first spreadsheet.// TODO: Choose a worksheet more intelligently based on your// app's needs.WorksheetFeed wsFeed = spreadsheet.Worksheets;WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];// Fetch the cell feed of the worksheet.CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);cellQuery.MinimumRow = 2;cellQuery.MinimumColumn = 1;cellQuery.MaximumColumn = 2;CellFeed cellFeed = service.Query(cellQuery);// Iterate through each cell, printing its value.foreach (CellEntry cell in cellFeed.Entries){// Print the cell's address in A1 notationConsole.WriteLine(cell.Title.Text);// Print the cell's address in R1C1 notationConsole.WriteLine(cell.Id.Uri.Content.Substring(cell.Id.Uri.Content.LastIndexOf("/") + 1));// Print the cell's formula or text valueConsole.WriteLine(cell.InputValue);// Print the cell's calculated value if the cell's value is numeric// Prints empty string if cell's value is not numericConsole.WriteLine(cell.NumericValue);// Print the cell's displayed value (useful if the cell has a formula)Console.WriteLine(cell.Value);}}}}?></div></div></body></html>

But I've also tried this, which I was playing with :

$file = "https://docs.google.com/a/emich.edu/spreadsheets/d/1OAFhMrayek9BKvO6ZI9EIPmCeY61oLL7-wfZOIg2xV8/edit#gid=724477237"$data = file_get_contents($file) or die('Could not read file!');echo $datafunction readRows() {var sheet = SpreadsheetApp.getActiveSheet(Eateries Menu Pricing.xlsx);var rows = sheet.getDataRange(A2:22);var numRows = rows.getNumRows(22);var values = rows.getValues();for (var i = 0; i <= numRows - 1; i++) {var row = values;Logger.log(row);}};var ss = SpreadsheetApp.getActiveSpreadsheet();var sheet = ss.getSheets()[0];// This represents ALL the datavar range = sheet.getDataRange();var values = range.getValues();//This logs the spreadsheet in CSV format with a trailing commafor (var i = 0; i < values.length; i++) {var row = "";for (var j = 0; j < values.length; j++) {if (values[j]) {row = row + values[j];}row = row + ",";}Logger.log(row);}

And finally I've tried this just to try displaying a website thinking it'll work:

<?php$myvar = "https://docs.google.com/a/emich.edu/spreadsheets/d/1O8-h-xmGE49K4x9sHrRL-c 97bHPXhma2eKkqrYAvog/edit#gid=1568863245";$var = fopen($myvar,"rb");echo stream_get_contents($var);?>

DISCLAIMER: I understand that somewhat similar-ish questions have been asked before, but I am so new to PHP that I really need to be talked through this process. I've been looking everywhere online to try to understand this all, and I'm really trying to understand the process and how this all works

Link to comment
Share on other sites

There are a couple obvious problems first. On top, remove this:

<!--#include file="php-5.6.2.tar.bz2" --><?php include("php-5.6.2.tar.bz2"); ?>
You're trying to tell it to include a BZ2 compressed archive. Include and require statements are only for PHP scripts or other text, you're telling it to include a binary archive which would just cause it to spit out the binary contents of that file in the browser.Secondly, you have this:
<?phpusing Google.GData.Client;using Google.GData.Spreadsheets;namespace MySpreadsheetIntegration{
The code you're showing there is not PHP code, that's either Java or C#. You can only put PHP code inside PHP blocks, PHP won't execute another language. Your other PHP code block also isn't all PHP code, it looks like you're copying and pasting things from several languages to try and get it to work, it looks like you have Javascript mixed in there.You might need to take a step back and go over the introductory PHP tutorials and things like that. You're going to have a difficult time doing something like this if you don't understand what PHP is and how it works, and you're probably only going to make yourself frustrated if you're just copying and pasting code from different places and trying to get it to all work together. You really need to understand how it all fits together rather than just copying and pasting. You might want to go to oreilly.com or somewhere similar and look into some books on PHP.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...