Using Google Sheets API to fetch the value of a single cell from a publicly accessible spreadsheet

Currently, I am working on developing a simple WordPress plugin using PHP and JavaScript. The main goal of this plugin is to create a widget. If you are not familiar with how WordPress plugins work, don't worry - the implementation is not too complex since regular JS/PHP can be used. In fact, the solution to my question may lie within a single PHP/HTML file.

I have a publicly-accessible Google Sheet with specific sharing settings:

https://i.sstatic.net/vCIbo.png

What I aim to do now is fetch the value stored in a particular cell on a designated worksheet. The targeted worksheet is named Live Summary and the cell of interest is E20.

https://i.sstatic.net/8lgRL.png

Although I have managed to retrieve this data using oAuth 2.0 authentication, it is not ideal for users to authenticate just to view this information.

My objective is to seamlessly display the contents of this cell on my website at all times, almost as if it were directly sourced from the MySQL database where WordPress resides.

https://i.sstatic.net/tRSJN.png

I have come across suggestions about utilizing certain GET endpoints from the Google Sheets API, but without utilizing oAuth tokens, I am uncertain about where to begin.

If feasible, could someone please: a) confirm whether this is achievable, and b) provide guidance on how to kickstart this process?

Preferably, I would like to use JavaScript for this task, though PHP would also suffice.

Answer №1

Follow these steps to make it work without OAuth.

1) Ensure your sheet is public for anyone on the web to access, as you have already done.

2) Publish it by going to File > Publish on the web in the Google Sheets UI.

3) Select Link, Entire Document, Web Page as the type and click Publish.

Once that's done, you can still utilize the legacy API detailed in https://developers.google.com/sheets/api/v3/worksheets to retrieve your data.

The URL format to access the data should be like this:

https://spreadsheets.google.com/feeds/<WHAT_KIND_OF_DATA>/<YOUR_SHEET_KEY>/<YOUR_SPREADSHEET_SHEET:)>/public/full?alt=json

For instance, if you want data from a sheet I've published (which has "I'm E20" at cell E20), you can find it here:

You also have the option to publish only specific data, which may better suit your needs. Nevertheless, I hope this helps you move forward.

UPDATE: Additional Steps:

You might encounter CORS as the next challenge. You can circumvent it using JSON-P. Below is a functional JSON-P solution to load content from the E20-cell in my sheet:

<html>
<body>
  <pre id='data'></pre>
</body>
<script>
  const onDataLoaded = (data) => {
    const e20Content = data.feed.entry.find((entry) => entry.title.$t == 'E20').content.$t
    document.getElementById('data').innerHTML = e20Content
  }
</script>
<script src="https://spreadsheets.google.com/feeds/cells/1TbMrtJl01i-Q5YudlhdAB_E1LTYkkLswnql5LHyiIuk/1/public/basic?alt=json-in-script&callback=onDataLoaded"></script>
</html>

Answer №2

After years of effectively utilizing this method, I have discovered that it is no longer yielding the desired results. - Ken B

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing a more efficient method for incorporating UUIDs into loggers

------------system1.ts user.on('dataReceived',function(data){ uniqueId=generateUniqueId(); system2.processData(uniqueId,data); }); ------System2.ts function processData(u ...

Guide on attaching an event to every dynamically created element in JavaScript

I'm currently generating 'li' elements dynamically using a loop and running into issues when it comes to assigning events to each generated element. My goal is to assign an onclick event to every li element that is created. Check out the co ...

Running JavaScript function from AJAX response containing both HTML and JavaScript code

For my first time using AJAX to prevent page refresh upon form submission, everything works flawlessly. The data is received in HTML form and placed into the designated div. However, I am encountering an issue with one of the JavaScript functions responsib ...

Synchronizing validation processes across both front-end and back-end applications

How do you ensure validations remain synchronized between front-end and back-end teams when working with laravel? While I found some information in this post, I am curious about how it specifically applies to laravel. I came across the laravel js validat ...

To ensure a rectangular image is displayed as a square, adjust its side length to match the width of the parent div dynamically

How can I make the images inside my centered flexbox parent div, #con, be a square with side length equal to the width of the parent div? The image-containing div (.block) is positioned between two text divs (#info and #links). I want the images to be squa ...

Tips for preserving the integrity of square brackets while extracting data from JSON

Everyone: We've decided to utilize Newtonsoft JSON.NET for serializing some C# POCOs, and here's what we have: { "RouteID": "123321213312", "DriverName": "JohnDoe", "Shift": "Night", "ItineraryCoordinates": [ [ 9393, 44 ...

JavaScript News Scroller for Horizontal Display

After searching through numerous websites, I have yet to find the ideal horizontal news scroller for my website. My specific requirements include: Must provide a smooth scrolling experience Scroll from right to left covering 100% width of the browser ...

obtain information from a Java servlet on a web page

I am working on a webpage that displays various coordinates (longitude, latitude), and I need to create a Java class to sort these coordinates. My plan is to send the coordinates to a Java servlet before displaying them on the webpage. The servlet will th ...

What is the best way to implement dynamic filtering in Nest JS?

I have a unique software application that requires dynamic filtering, but I am facing a challenge with different fields where I cannot specify any field specifically. Can you provide me with some guidance or articles on how to tackle this issue? Here is a ...

Monitoring changes to localstorage in AngularJS

How can I monitor changes to localStorage using $watch? I have created a factory to simplify setting and getting values .factory('$localstorage', ['$window', function($window) { return { set: function(key, value) { ...

Is there a way to align two canvases perfectly on top of each other?

My current challenge involves the need to position one HTML5 canvas on top of another. So far, I have successfully accomplished this using the following method: <canvas id="lower" width="900" height="550" style="position: absolute; left: 0; top: 0; z-i ...

Resuming AJAX requests after being aborted

Hey everyone, I'm curious to know if it's possible to resume interrupted ajax requests. I have an array of ajax calls stored as defferreds like this: var defferreds = []; defferreds.push( $soap.ajax({ type: "POST", dataType: ...

Undefined is returned when exporting an item from the function called 'this'

Here we have two examples of JavaScript functions. In the first one, console.log(this) works as expected and returns methods, variables, etc. function foo() { console.log(this); } foo(); However, in the second example: export const something = ' ...

Issue encountered when integrating JavaScript into Django view form update

I have successfully implemented a Django view to add an invoice to my application, complete with added JavaScript functionality that is functioning properly. <p> <label for="id_total_without_vat">Price</label> <input ...

"Trouble with kendo drop-down list: onclick event not triggering when text is changed

I am currently working with kendo UI and have implemented a dropdown list of checkboxes. The onchange event is triggering when the user clicks on the checkbox, but it is not firing when the user clicks on the text. Thank you in advance for your assistance ...

Using addClass and removeClass functions in JQuery when selecting a radio button

I am facing an issue with my radio buttons being displayed as images. When a picture (radio button) is selected, I want the other pictures to become hidden. To achieve this, I plan to add a class to the checked picture and then add another class to the unc ...

Tips for linking attributes to a function using ES6

I'm currently working on a stateless React.js component called myView. I want to keep the syntax as concise as possible, but I'm having trouble understanding how to export a default function with an attached object. Here's an example in myVi ...

What is the best way to adjust the size of carousel images within a box element?

How can I ensure that carousel pictures are displayed clearly within the box without overflowing? The code I tried resulted in the pictures overflowing from the box and not being visible clearly. How can I resolve this issue? .container { width: 1490px; ...

Is it possible to design a collapsible element that gives a sneak peek of its content before fully expanding?

In the title, I mentioned that I require the collapsible to display its content partially before expanding and then fully after expanding. To illustrate this concept, here are two drawings for reference: https://i.sstatic.net/f4CHh.png https://i.sstatic. ...

Vue.js parent component not receiving data emitted by child component

Below you will find the child and parent components. I am struggling to pass data from the child component to the parent component. Can you help me pinpoint where the issue may be? Child Component <template> <div> <v-btn class="r ...