2

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.

D.L
  • 4,339
  • 5
  • 22
  • 45
  • Possibly a CORS policy issue? Why not upload the file to Google Drive, and load/embed from there? – Peter Thoeny Dec 30 '22 at 22:46
  • @PeterThoeny. the text file already 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 side and the text document are in the same folder. – D.L Dec 30 '22 at 22:54
  • This here: https://stackoverflow.com/a/71409801/7475450 – Peter Thoeny Dec 30 '22 at 23:07
  • @PeterThoeny: so basically, according to the link posted, this needs to be done via an API ? – D.L Dec 31 '22 at 00:31
  • That answer is Google apps script specific. If you use that you likely want to text from the blob, e.g. `let html = UrlFetchApp.fetch(url, {...})).getBlob().getDataAsString();`. However, it looks like you run that code in the browser, so you could use a fetch using the Google drive URL of a file, but need to provide the proper authorization header with token – Peter Thoeny Dec 31 '22 at 00:44
  • Does this answer your question? [JavaScript read all text from txt file on Google drive or Dropbox](https://stackoverflow.com/questions/38447071/javascript-read-all-text-from-txt-file-on-google-drive-or-dropbox) – Rubén Jan 28 '23 at 15:59
  • @Rubén, that was from the same link that i tried to implement this into the google site (via `embed code`), but was unable to. I create a simple text file with "hello world" and for example want to get "hello" into the google site page. (ie. a very basic requirement). – D.L Jan 28 '23 at 16:28

1 Answers1

1

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • so if there is a public URL as in a google share link, would `fetch` then work ? – D.L Jan 28 '23 at 15:50
  • I think that using a link 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?](https://stackoverflow.com/q/15557392/1595451) – Rubén Jan 28 '23 at 16:04
  • images are actually fine. Once can create images and store them and google sites permits the upload of images (so when they are modified the image updates). The same is **so far not true for text** and this is what the question seeks to resolve. – D.L Jan 28 '23 at 16:12
  • See my last edit to the answer and the suggested original. – Rubén Jan 28 '23 at 16:13