【问题标题】:Saving image to Spreadsheet with Google Scripts使用 Google 脚本将图像保存到电子表格
【发布时间】:2017-03-27 19:24:25
【问题描述】:

我正在尝试使用 jSignature 将签名板添加到 Google 表格。我添加了一个对话框,记录这样的签名:

//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()" />

唯一的问题是我不知道如何将图像放入单元格中。复制/粘贴不起作用,据我所知,它需要插入。我在想也许我编写了一个函数将其保存到 Google Drive,然后使用 URL 将其插入,但我仍然不知道如何获取实际图像以便对其进行任何操作。感谢任何见解,我是 GS 的新手。

【问题讨论】:

  • 你在文档的工作表页面上试过insertImage吗?
  • 还没到那一步,我不知道如何先从对话框中抓取渲染图像。 :\
  • This 可能会有所帮助。
  • 谢谢!我想首先我需要编写脚本保存图像,然后获取 URL。

标签: jquery google-apps-script google-sheets jsignature


【解决方案1】:

要将图像保存到您的云端硬盘,您可以执行以下操作

您的 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()" />

服务器端代码:

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)
}

您必须跟踪图像文件的名称并将图片相应地插入到电子表格中。

【讨论】:

  • 天哪,成功了!我发誓我之前使用的是 DriveApp,并且不断收到未定义“DriveApp”的错误。我是怎么了!我可以使用相同的文档将插入图像上传到电子表格吗?
  • 可能是拼写错误,使用 Driveapp 或 DriveApp?不管怎样,很高兴它成功了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多