如何在 .运行 函数中传递 blob

How to pass a blob in a .run function

我在 Google Apps Script Reference 中找不到答案,所以我想问:能否通过 [=36] 中的 .运行() 函数传递 blob =] 脚本。我的代码如下。

这是我的 HTML 文件的脚本。所有 HTML 包含的都是一个对象。还有一个

来制作 "print" 语句。

<script>
  var canvas = document.getElementById('myCanvas');
  var can = canvas.getContext("2d");
  can.fillRect(20,20,150,100);
  var canvasData = canvas.toDataURL('image/png', 1);
  var blob = toBlob(canvasData);

  document.getElementById('ins').innerHTML = "Got the Blob  " + blob.type;
  google.script.run.printCanvas(blob);


  function toBlob(dataURI) {

    // convert base64 to raw binary data held in a string
    // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
    document.getElementById('ins').innerHTML = ("Data URI:" + dataURI);

    var byteString = atob(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

    // write the bytes of the string to an ArrayBuffer
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }

    // write the ArrayBuffer to a blob, and you're done
    var blob = new Blob([ab], {type: 'image/png'});

    return blob;
  }

</script>

blob.type的"print",给了我"Got the Blob image/png"

但是,应用程序不会执行 .运行(blob),错误代码:

"Uncaught TypeError: Failed due to illegal value in property: 0"

我知道 GAS 不会传递 DIV,所以我想知道是否无法将 blob 传递给 .gs 文件。我不知道为什么应用程序不会 运行。如果可以,请告诉我怎么做。

我知道这不是 .gs 的一面,因为 .gs 的第一行是 DocumentApp.alert,它不会弹出。因此,应用程序未到达 .gs 文件。

所以,感谢 Sandy Good。事实上,答案是否定的。无法按照所述将 blob 发送到 .gs 页面。

但是,您可以将 blob 编码为 base-64 编码字符串,

https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

然后在.gs文件中用Utilities.base64Decode(base64encodedstring)进行解码;