Create (draw) barcode image from array in google doc using google script

∥☆過路亽.° 提交于 2019-12-08 13:17:28

问题


I need to generate and paste barcode image in a google doc. I've found out how to create a js array, which represents it, using google script. But have no idea how to create and paste an image from this array into google doc. Although it's easy to create an SVG image in a simple web page.

Can you please help me?


回答1:


If you can get something like a jQuery pluggin to work with the new iFrame HTML service, then you could create the bar code in HTML, then save the image to an image file. Then get the file as a blob.

If the new iFrame HTML Service will accept a pluggin, and allow a Canvas tag, it could work that way.

I don't know of a way to code your own JavaScript to make the bar code. I think you'd want to start with seeing what is available out there on the internet.

You are asking a very broad question, that would require something like a tutorial to provide an answer.

Code to insert image from Original Poster:

function insertImage() {
  var baseUrl = canvas.toDataURL("image/png");
  var resp = UrlFetchApp.fetch(baseUrl);

  doc.getChild(0).asParagraph().appendInlineImage(resp.getBlob());
};

Original Generic function:

function insertImage() {

  var Doc = DocumentApp.openById('Your Doc ID here');
  var image = DocsList.getFileById('Your Image ID here');

  // Use this if the Doc is open and code running is bound to this doc
  //var body = DocumentApp.getActiveDocument().getBody();
  var body = Doc.getBody();
  var whatParagraph = 3; //Insert an image in the fourth paragraph

  body.insertImage(image);// 'image' is the image file as blob
  //body.appendImage(whatParagraph, image); // Either insert or append can be used
}



回答2:


You have accepted answer, but as an alternative I'd be tempted to hand off the barcode generation to a published API. There are a few.

e.g. http://www.scandit.com/2012/12/11/create-your-own-barcodes-with-scandits-new-barcode-generator/

Reason being that liability caveats aside, there is a maintained implementation test by results. Not to judge your ability, just thinking of good practice.



来源:https://stackoverflow.com/questions/28095001/create-draw-barcode-image-from-array-in-google-doc-using-google-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!