2017-03-27 25 views
0

jSignatureを使用してGoogleシートに署名パッドを追加しようとしています。Google Scriptでスプレッドシートに画像を保存する

//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ドライブに保存してURLを使用して挿入する機能を作成しても構わないと思っていましたが、実際の画像を取得する方法を把握することはできません。どんな洞察も高く評価されています。

+1

ドキュメントのシートページで[insertImage](https://developers.google.com/apps-script/reference/spreadsheet/sheet)を試しましたか? – Cooper

+0

これまでのところ、私はダイアログボックスからレンダリングされたイメージを取得する方法を最初に理解できませんでした。 :\ – Lauren

+0

[This](http://stackoverflow.com/questions/10673122/how-to-save-canvas-as-an-image-with-canvas-todataurl)が役立つ可能性があります。 – Cooper

答えて

1

あなたはこの

のHTMLコードのような何か行うことができますあなたのドライブに画像を保存するには:あなたはトラックを保持する必要があります

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

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

サーバー側のコードを画像ファイルの名前を入力し、それに従って画像をスプレッドシートに挿入します。

+0

ああ私の良さ、それは働いた! DriveAppを以前使用していて、「DriveApp」が定義されていないというエラーが発生していると誓っています。私は一体どうしたんだろう!同じ文書を使用して画像をスプレッドシートに挿入することはできますか? – Lauren

+0

ありがとう、ここであなたの助けを借りて私はそれが働いている! – Lauren

+0

代わりにDriveappを使用したか、DriveAppを使用したかもしれませんか?どちらにしても、うれしい –