Insert image from URL into Google Docs

hbbz040

I am trying to get image from external link and add in a document.

Html service work fine. However, document should fill out after onload function.

How can I fix it ?

code.gs

function doGet(e){
  var content = HtmlService.createHtmlOutputFromFile('index').getContent();
  return HtmlService.createHtmlOutput(content);
}

function im(baseUrl){
  var resp = UrlFetchApp.fetch(baseUrl); 
  var docID = "0000xxxxx1111"
  var doc = DocumentApp.openById(docID);
  doc.getChild(0).asParagraph().appendInlineImage(resp.getBlob());
  return baseUrl; 
}

index.html

<!DOCTYPE html>
<head>
<base target="_top">
<script>
  window.onload = function() {
    var url = "http://xxx.co/image.png"
    var canvas = document.createElement('canvas');
    canvas.width = 200;
    canvas.height = 100;
    document.getElementById('barcode').appendChild(canvas);
    var ctx = canvas.getContext('2d');
    var image = new Image();
    image.src = url;
    ctx.drawImage(image, 0, 0);
    google.script.run.withSuccessHandler(function(a) {
       document.getElementById("imageid").src=a;
    }).im(canvas.toDataURL());
    }
</script>
</head>
<body>
   <img id="imageid" src="" alt="image">
</body>
</html>
Jordan Rhea

There are a few problems with your HTML which is why it probably isn't working. You also had not tagged any elements with the barcode id.

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>

  <body id="barcode">
     <img id="imageid" src="" alt="image">
  </body>

  <script>
    window.onload = function() {
      var url = "http://lorempixel.com/400/200/"
      var canvas = document.createElement('canvas');
      canvas.width = 200;
      canvas.height = 100;
      document.getElementById('barcode').appendChild(canvas);
      var ctx = canvas.getContext('2d');
      var image = new Image();
      image.src = url;
      ctx.drawImage(image, 0, 0);
      google.script.run.withSuccessHandler(function(a) {
         document.getElementById("imageid").src=a;
      }).im(canvas.toDataURL());
    }
  </script>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert Image (from URL) from Google Sheet Cell to Google Doc

From Dev

How to import csv file from url into Google Docs (Drive)

From Dev

how to insert image from url in markdown

From Dev

How to insert an image into Word from a URL

From Dev

Insert byte format of image from url

From Dev

VBA macro to insert image from URL in PowerPoint

From Dev

How to insert an image into Word from a URL

From Dev

How to insert and center text in Google Docs with script

From Dev

Google docs: Insert TOC through scripting

From Dev

How do I get Google Docs/atachment URL from a Google Calendar event?

From Dev

Insert Image by URL Doesn't Work in Google Apps Script

From Dev

Insert Image by URL Doesn't Work in Google Apps Script

From Dev

Insert Image by URL Doesn't Work in Google Apps Script

From Dev

Convert InlineImage in Google Docs to an HTML image

From Dev

How to add image numbers in Google Docs

From Dev

How to view image using google docs?

From Dev

Python3 - Download file from google docs private and public url

From Dev

Calling a function inside an URL in Google Docs Spreadsheet

From Dev

Android Google Maps Overlay image from URL

From Dev

PHP: get a qr image from google charts + insert in MySQL database

From Dev

How to upload an image to Google Cloud Storage from an image url in Node?

From Dev

Accessing Google docs from a chrome extension

From Dev

Android XMPP Server in Java From Google Docs

From Dev

Modify a table from script in Google Docs

From Dev

Getting data from Google docs into Javascript

From Dev

Get total hours from google docs timestamp

From Dev

Google images: get large image URL from thumbnail URL

From Dev

Manipulate PositionedImage and wrap text around image in Google Docs

From Dev

How to get youtube stream url in form of docs.google.com

Related Related

  1. 1

    Insert Image (from URL) from Google Sheet Cell to Google Doc

  2. 2

    How to import csv file from url into Google Docs (Drive)

  3. 3

    how to insert image from url in markdown

  4. 4

    How to insert an image into Word from a URL

  5. 5

    Insert byte format of image from url

  6. 6

    VBA macro to insert image from URL in PowerPoint

  7. 7

    How to insert an image into Word from a URL

  8. 8

    How to insert and center text in Google Docs with script

  9. 9

    Google docs: Insert TOC through scripting

  10. 10

    How do I get Google Docs/atachment URL from a Google Calendar event?

  11. 11

    Insert Image by URL Doesn't Work in Google Apps Script

  12. 12

    Insert Image by URL Doesn't Work in Google Apps Script

  13. 13

    Insert Image by URL Doesn't Work in Google Apps Script

  14. 14

    Convert InlineImage in Google Docs to an HTML image

  15. 15

    How to add image numbers in Google Docs

  16. 16

    How to view image using google docs?

  17. 17

    Python3 - Download file from google docs private and public url

  18. 18

    Calling a function inside an URL in Google Docs Spreadsheet

  19. 19

    Android Google Maps Overlay image from URL

  20. 20

    PHP: get a qr image from google charts + insert in MySQL database

  21. 21

    How to upload an image to Google Cloud Storage from an image url in Node?

  22. 22

    Accessing Google docs from a chrome extension

  23. 23

    Android XMPP Server in Java From Google Docs

  24. 24

    Modify a table from script in Google Docs

  25. 25

    Getting data from Google docs into Javascript

  26. 26

    Get total hours from google docs timestamp

  27. 27

    Google images: get large image URL from thumbnail URL

  28. 28

    Manipulate PositionedImage and wrap text around image in Google Docs

  29. 29

    How to get youtube stream url in form of docs.google.com

HotTag

Archive