1

Background: I have a Google Sheet that automatically manages key organization event dates, but occasionally one has to be manually overridden. I have been duplicating those auto calc'd event dates using PHP on the organization's web page but that doesn't handle it when the event dates are manually changed back in the Sheets database.

Need Statement: I wish to grab the text content of the Google "DateCalc" Sheet, cell "A33" (which contains the corrected next Date) and embed it automatically in a web page sentence that states, "The next event is scheduled for ???." where "???" is whatever is needed for the HTML embed effort for the text content of cell A33.

All the embedding approaches I have tried using the publish to web feature of Sheets, just display a frame with the entire Sheets page content, and I have not been able to find a simple single text display approach, which is what I seek. All the solutions I have found so far are for much more complex situations. I believe I don't need any more code on the Sheet (source) end, just an appropriate "embed code snippet" at the destination end.

I can embed a complete sheet but haven't found a way to embed a simple text string in line with the surrounding text in the sentence.

This web script is the closest I have been able to come...

Our next meeting is on <object data="https://docs.google.com/spreadsheets/d/e/2PACX-1vQq6sXBel4SOW5U_XLnQ1xyyl4P-aK2v1R-5uofUvstMLV89_yCSqX3Lmsy4B7E21gojeG88efGFjZ0/pubhtml?gid=604484136&amp;single=true&amp;range=A29&amp;chrome=false" type="text/html" width="171" height="15"></object>.

but, if the Code Snippet really ran (I changed the file ID to protect the innocent), you would see it doesn't produce an in-line text string but an isolated "block" with the text inside it, as shown below....

enter image description here

The text seems to be an image, not text, that is displaced above the text baseline and has a small gray blob following it that I just can't get rid of.

Marios
  • 26,333
  • 8
  • 32
  • 52
burky39
  • 65
  • 1
  • 9
  • I think you could use the Advanced Google Sheets API using PHP to get this data directly take a look at this https://stackoverflow.com/questions/20407579/how-do-i-access-the-google-spreadsheets-api-in-php and here is a quikstart tutorial https://developers.google.com/sheets/api/quickstart/php and I also think you could do it with a webapp and the contentService by doing a get with Javascript (HTTP request) – Cooper Sep 16 '19 at 19:06
  • 1
    I also agree publishing a [web-app](https://developers.google.com/apps-script/guides/web) is the easiest approach. BTW, Doesn't publishing the sheet already expose the innocent? – TheMaster Sep 16 '19 at 20:38

3 Answers3

2

I'll assume that your server uses a combination of PHP and HTML, so one approach can be to use that PHP code to make a call to the Sheets API to retrieve the data. In the PHP Sheets API Quickstart you can find a example of how to set up a project to achieve a working connection between your server and the API. After you complete the tutorial, you can modify your script to include the following function so you can achieve your original request.

function getNextDate() {
  $client         = getClient();
  $sheetsService  = new Google_Service_Sheets($client);
  $spreadsheetID  = '{YOUR SPREADSHEET ID}';
  $cellRange      = 'DateCalc!A33';
  $serverResponse = $sheetsService->spreadsheets_values->get($spreadsheetID, $cellRange);
  $cellValue      = $serverResponse->getValues();
  if (empty($cellValue)) {
    return "NO MEETINGS AHEAD!\n";
  } else {
    foreach ($cellValue as $value) {
      return $value[0];
    }
  }
}

A totally different alternative is to use a webapp. You can use this tutorial to deploy a webapp that can be later embedded in your webpage to show the following meeting dates.

So there are two free alternatives to accomplish your objectives: using the Sheets API or a webapp; and in this anwser I provided a function for the API option. Please, don't hesitate to offer information for further clarifications or request for help.

Jacques-Guzel Heron
  • 2,480
  • 1
  • 7
  • 16
  • I am not badly confused as I don't see how the SpreadSheet and the web page can communicate with each other. It appears to me that when a "Web Ap" is created in the Sheet it is tied closely with the sheet and exists on my Google Drive. But my web page is far away on a GoDaddy server. Seems to me that makes what has been suggested so far to be inoperable. Also this all applies to a little senior citizen club with a tiny budget, so program subscriptions (even $15/month) exceed 10% of the budget so I need a truly non-complex and unsophisticated solution. :-) – burky39 Sep 17 '19 at 18:59
  • That should be "I am NOW badly confused" Forum would not let me edit that correctly. – burky39 Sep 17 '19 at 19:06
  • Hi there @burky39, I just updated my answer to show a clear distinction between both options: the API and the webapp. Let me know if you need more info. – Jacques-Guzel Heron Sep 18 '19 at 11:48
1

Our next meeting is on .

I built a free, open source, and easy to use service and badge generator called https://cellshield.info. It works out of the box for public Google spreadsheets. What you see above is a live Markdown output version of a badge that is synced from a spreadsheet with the value formatted as what was shown in your example with a formula of TODAY(). The Markdown just makes an image tag which I suppose you can do with anything else that can make an image tag as well.

If you want the yellow or any formatting to to go away, try this:

Our next meeting is on .

This was done by adding &color=rgba%28255%2C255%2C255%2C0.0%29&style=flat-square to the arguments. The text doesn't match perfectly, it's servicable.

Anyway, I see you went with tabletop but if you or anyone else is just looking to do what you wanted to do, then my service should be sufficient.

For example, it is used for this Game Boy development contest to present the prize pool amount.


If a private spreadsheet solution is needed, one could fork my code, run it on Cloud Run in their Google Cloud Platform account and explicitly share the spreadsheet to the service account on their Google Cloud Platform instance.

nelsonjchen
  • 365
  • 6
  • 16
0

Jacques- Merci beaucoup !

It has been a challenging several days of non-stop effort but you put me on the right track.

Using less than 20 lines of JavaScript code, including one line for the free TableTop.js library call (see https://github.com/jsoma/tabletop#if-your-publish-to-web-url-doesnt-work) and inserting a string in the middle of my HTML sentence, yielded EXACTLY what I was seeking and the solution was INFINITELY SIMPLIER than virtually everything else I found that required multiple libraries, subscription fees, complex system calls, etc.

The hardest part was inserting the JavaScript code into WordPress which my web site is constructed with, as you can essentially only insert plain text into a WordPress page, but with the an added WordPress plugin (Insert Header and Footers) you can put code into a WordPress page.

Many thanks for getting me headed in the right direction, and having a great learning experience along the way. :-)

burky39
  • 65
  • 1
  • 9