问题
I have been working on making projects inside Google Apps Script but came across a problem.
I am currently trying to get the individual pixels of an image, by using an HTML canvas(if you know of another way to get pixel data from an image in JS, please let me know). The problem is, Google Apps Script doesn't seem to have a getContext method for the canvas. I'm not sure if this is done purposely, or if it is some kind of bug.
Code:
var img = XmlService.createElement('img');
img.src = query;
var canvas1 = XmlService.createElement('canvas');
var context = canvas1.getContext('2d');
Error:
TypeError: canvas1.getContext is not a function (line 8, file "Code")
I am not sure how to fix or get around this.
回答1:
You created an element with: var canvas1 = XmlService.createElement('canvas');
These are the methods for an element
回答2:
The "canvas element" your are asking for is part of Canvas API.
Google Apps Script doesn't include functions for the canvas element, actually it doesn't include any function for any Web APIs elements.
The XMLService Service has the Class Element, this is
A representation of an XML Element node.
来源:https://stackoverflow.com/questions/63219204/canvas1-getcontext-is-not-a-function-error-when-creating-a-canvas-element-via