Andrea Volontè
Andrea Volontè

Reputation: 85

Read Google Spreadsheet cell value from external page

in a Spreadsheet I have in cell A1 a number. I know how to read it and use in a html file created inside the script ( with HtmlService)

Is there a way to use the data in an external page?

On my site I'm trying to have somethin like:

<p>In your bag there are</p> + "cellA1Value" + <p>carrots</p>

Ask if I have not been clear. Thanks

Upvotes: 0

Views: 606

Answers (1)

Cooper
Cooper

Reputation: 64032

Reading a value from an External Page

You will have to put the appropriate value in Sheet1!A1. You will have to deploy as webapp and give access to anyone, even anonymous. Then everytime you load your page it loads that value into <span id="spn1"></span>

Here's the doGet():

function doGet(e){
  if(e.queryString !=='')
  {
    var s=SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange(e.parameter.range).getValue();
  }else{
    var s='No Query Parameter Received';
  }
  return ContentService.createTextOutput(s);
}

Here's my external website code:

<html>
    <head>
        <title>Testing</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
         $(function(){
            var url='https://script.google.com/macros/s/AKfycbxjlJNGYL65mItHLoL76jiEWX4iQlKzpywMAjg5SMgUUUZNbm8/exec?range=A1';
            $.get(url,function(data,status){
                document.getElementById('spn1').innerHTML=data;
            });
        });
            console.log('MyCode');
        </script>
     </head>
     <body>
        <div id="mydata">The size of my ruler is <span id="spn1"></span> inches.</div>
     </body> 
</html>

You could use a multicell range and return a delimited string, split it and then loop through the array assigning values to different spans.

Upvotes: 1

Related Questions