Is it possible to read the contents of Google spreadsheet from external HTML?

Asked 2 years ago, Updated 2 years ago, 49 views

I'm a beginner

Is it possible to read the contents of Google spreadsheet into external HTML?
(Example: Place A1 in the spreadsheet in the HTML P tag)
I was able to find the document that you said you would load into the HTML of GAS.
I'm asking you a question because I can't find anything to read from the external HTML.

If possible, I would appreciate it if you could let me know

Thank you for your cooperation

html google-apps-script

2022-09-30 17:57

1 Answers

Google spreadsheet content cannot be set directly to external HTML.
However, it is possible to:(Please change the settings in a timely manner)

  • GAS Menu → Published → Deployed as a Web Application → Update All Users Who Have Access to the Application (Including Anonymous Users)
  • ↑Copy URL

The actual code will look like this:

GAS

var SHEET_NAME="Sheet 1";
function getValue() {
  vars = SpreadsheetApp.openById(SpreadsheetApp.getActiveSpreadsheet().getId());
  varsheet=ss.getSheetByName(SHEET_NAME);
  return sheet.getRange("A1").getValue();
}

function doGet(e){
  return ContentService.createTextOutput(getValue()) .setMimeType(ContentService.MimeType.TEXT);
}

JavaScript

#↑ copied URL
consturl="https://script.google.com/macros/s/xxxxxxxxxxx/exec"
fetch(url,{
  method: "GET",
}).then(response=>response.text())
.then(text=>{
  // Output retrieved value to console
  console.log(text);
  // Set the required values in HTML.
  const targetID = "hoge";
  document.getElementById(targetID).innerText=text;
});

You can hit the API on the server side even if it's not JavaSciplt.
If the server handles it in the first place, I think there is a way to go read the spreadsheet directly using Google API instead of using GAS.
(I'm not familiar with Java, but I think I can use the library Google-api-java-client.)


2022-09-30 17:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.