简体   繁体   English

使用 Google Scripts 将图像保存到电子表格

[英]Saving image to Spreadsheet with Google Scripts

I'm trying to add a signature pad to a Google Sheet using jSignature.我正在尝试使用 jSignature 将签名板添加到 Google Sheet。 I've added a dialog box that records the signature like this:我添加了一个记录签名的对话框,如下所示:

//Code.gs
function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Page')
    .setWidth(400)
    .setHeight(300);
DocumentApp.getUi()
  .showModalDialog(html, 'Your Signature is Required');
}

//Page.html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/willowsystems/jSignature/master/libs/jSignature.min.js"></script>

Please draw your signature on the signature pad below: 

<div id="signature"></div>

<img id="rendered" src="">

<script>
  $("#signature").jSignature({
    'background-color': 'transparent',
    'decor-color': 'transparent'
  });

  function renderSignature(){
    $("img#rendered").attr("src",$('#signature').jSignature('getData','default'));
  }
</script>

<input type="button" value="Render" onclick="renderSignature();"/>
<input type="button" value="Add to Sheet" onclick="//What to do"/>
<input type="button" value="Close" onclick="google.script.host.close()" />

The only thing is I can't figure out how to get the image into a cell.唯一的问题是我不知道如何将图像放入单元格中。 Copy/paste won't work, it would need to be inserted as far as I can tell.复制/粘贴不起作用,据我所知,它需要插入。 I was thinking maybe I write a function to save it to Google Drive and then insert it using a URL, but I still can't figure out how to grab the actual image in order to do anything with it.我在想也许我写了一个函数将它保存到谷歌驱动器,然后使用 URL 插入它,但我仍然无法弄清楚如何抓取实际图像以便对其进行任何操作。 Any insight appreciated, I'm new to GS.任何见解表示赞赏,我是 GS 的新手。

To the save the image to your Drive you can do something like this要将图像保存到您的云端硬盘,您可以执行以下操作

Your Html Code:您的 HTML 代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/willowsystems/jSignature/master/libs/jSignature.min.js"></script>

Please draw your signature on the signature pad below: 

<div id="signature"></div>

<img id="rendered" src="">

<script>
  $("#signature").jSignature({
    'background-color': 'transparent',
    'decor-color': 'transparent'
  });

  function renderSignature(){
    $("img#rendered").attr("src",$('#signature').jSignature('getData','default'));
  }

  function saveImage(){ //This sends the image src to saveImages function
  var bytes = document.getElementById('rendered').src
  console.log(bytes)
  google.script.run.saveImage(bytes)
  }
</script>

<input type="button" value="Render" onclick="renderSignature();"/>
<input type="button" value="Add to Sheet" onclick="saveImage()"/>
<input type="button" value="Close" onclick="google.script.host.close()" />

Server side code:服务器端代码:

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Sign')
    .setWidth(400)
    .setHeight(300);
SpreadsheetApp.getUi()
  .showModalDialog(html, 'Your Signature is Required');
}

function saveImage(bytes){
  var bytes = bytes.split(",")
  var blob = Utilities.newBlob(Utilities.base64Decode(bytes[1]), 'image/png');
  blob.setName("Sign Pic")
  DriveApp.getFolderById("Folder ID to save SignPic").createFile(blob)
}

You will have to keep track of names of image files and insert the pics into the spreadsheet accordingly.您必须跟踪图像文件的名称并将图片相应地插入到电子表格中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM