insert data from a text file into a google site

450 views Asked by At

I am trying to insert text from a *.txt file into a google site.

this is what i have tried so far (using fetch), but my search is exhausted:

<html>

<body>
<div id="text-container"></div>

<script>
  fetch("G:/My Drive/path/to/test_file/test.txt")
    .then(response => response.text())
    .then(text => {
      document.getElementById('text-container').innerHTML = text;
    });
</script>
</body>

</html>

But all I get is a blank.

I know the text file exists because i can embed the whole file (from the same location), but i just want to extract the relevant text into the google site and not the whole file (which is an undesired alternative).

I see that other answers that require GOOGLE API (from 2017) are potentially available here as a workaround, but do not appear to work now (in 2023): JavaScript read all text from txt file on Google drive or Dropbox

But this would be rather cumbersome for extracting small pieces of text.

How can this be achieved?


Note: The text file lives in Google Drive. I can embed the entire file (which looks rather ugly, link to a file), so I want to take just the text content into the html... both the Google Site and the text document are in the same folder.

1

There are 1 answers

4
Wicket On

fetch can't access a file stored in a local unit. It should use an URL (not local file path) or a request.

To access files stored in Google Drive you might have to use the Google Drive API or Google Apps Script (i.e. create a web app and use the Drive Service).


A link taken from the Google Drive user interface to the file will not work to directly get the text from a txt file. If you don't want to use the Google Drive API / Google Apps Script you could use hand crafted URL... -> How do I display images from Google Drive on a website?. While this question title mentions image, the hand crafting method works for other types for files. Note this is a hacky solution, it might be great for hobby / small projects but I don't think that it should be used in large projects.


Reference


Related

Grouped by tag used to in the search query