Google Apps Script for Google Docs shows permission denied

68 views
Skip to first unread message

Stephan Kamstra

unread,
Jun 29, 2025, 9:04:25 AMJun 29
to Google Apps Script Community
I am trying to create a script (on another account) using Google Apps Script. But somehow it gives me an error:

Error when sending data: ScriptError: There is a server error while reading from the storage. Errorcode PERMISSION_DENIED.

What I was trying to do is using the Code.gs and an html file.

The html file has a canvas on it, allowing me to "draw" on it and having that drawning (signing) on the document I need it to be on.

It works till the part of drawing, but when I press the button to add that drawing base64 bit on the doc it gives me the above error.

This is the function that should add the base64 to the doc, coming from the form (sorry tekst is in dutch):

// Functie om de handtekeningafbeelding (getekende paraaf) in te voegen
function addImageSignature(imageData) {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();

console.log('test');
try {
// Log de ontvangen dataUrl voor debugging
Logger.log('Received data URL: ' + imageData);

// Controleer of imageData daadwerkelijk een base64 string is
if (!imageData || imageData.split(',').length < 2) {
throw new Error('Ongeldige data ontvangen. Geen base64 string.');
}

// Decode base64 afbeelding en voeg deze in het document in
var decodedImage = Utilities.base64Decode(imageData.split(',')[1]); // Verwijder de prefix "data:image/png;base64,"
var imageBlob = Utilities.newBlob(decodedImage, 'image/png');
// Voeg de afbeelding toe aan het document
body.appendImage(imageBlob);
DocumentApp.getUi().alert('Je getekende paraaf is toegevoegd!');
} catch (e) {
DocumentApp.getUi().alert('Er is een fout opgetreden bij het toevoegen van je getekende paraaf. Probeer het opnieuw.');
Logger.log('Error: ' + e.toString()); // Voor debugging
}
}

This is the html:

<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
#canvas {
border: 1px solid #000;
}
#clearButton {
margin-top: 10px;
}
</style>
</head>
<body>
<h3>Teken je paraaf:</h3>
<canvas id="canvas" width="400" height="200"></canvas>
<br><br>
<button id="clearButton" type="button" onclick="clearCanvas()">Wis het canvas</button>
<br><br>
<button type="button" onclick="submitSignature()">Voeg paraaf toe</button>

<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var drawing = false;

// Start tekenen
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseleave', stopDrawing);

function startDrawing(e) {
drawing = true;
draw(e);
}

function draw(e) {
if (!drawing) return;
ctx.lineWidth = 3;
ctx.lineCap = 'round';
ctx.strokeStyle = '#000';

ctx.lineTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
ctx.stroke();
}

function stopDrawing() {
drawing = false;
ctx.beginPath();
}

// Wis het canvas
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}

// Verstuur de getekende paraaf als afbeelding
function submitSignature() {
var dataUrl = canvas.toDataURL('image/png'); // Zet het canvas om naar base64-afbeelding (PNG)

// Debugging: Log de base64-string in de console voor controle
console.log("Data URL:", dataUrl);

// Log voor controle: Controleren of we de functie aanroepen
console.log('Verstuur naar Google Apps Script via google.script.run');
console.log(google.script.run.addImageSignature(dataUrl));

// Verstuur de data naar Apps Script
google.script.run
.withSuccessHandler(onSuccess)
.withFailureHandler(onFailure)
.addImageSignature(dataUrl); // Verstuur naar Apps Script om toe te voegen

// google.script.host.close(); // Sluit het dialoogvenster
}
function onSuccess(output) {
console.log('Succes! Response: ', response); // Log het antwoord van Apps Script
}

function onFailure(error) {
console.log('Fout bij het verzenden van de data:', error); // Log fouten als ze optreden
}
</script>
</body>
</html>

Some one able to assist.

Cheers


Stephan Kamstra

unread,
Jul 15, 2025, 3:56:56 AMJul 15
to Google Apps Script Community
Any one an idea?

I've set all permissions, I could think of needed for docs to work with the script.

Paulo Rorberto

unread,
Jul 15, 2025, 11:12:34 AMJul 15
to google-apps-sc...@googlegroups.com
bom dia,  tente assim com o script e modifique seu html  // *************** CÓDIGO DA ASSINATURA ***************
let desenhando = false;
const canvas = document.getElementById('assinaturaCanvas');
const ctx = canvas.getContext('2d');

// Configuração inicial
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = '#000000';
ctx.lineWidth = 2;

// Eventos para capturar o desenho
canvas.addEventListener('mousedown', iniciarDesenho);
canvas.addEventListener('mouseup', pararDesenho);
canvas.addEventListener('mouseleave', pararDesenho);
canvas.addEventListener('mousemove', desenhar);

// Para dispositivos touch
canvas.addEventListener('touchstart', iniciarDesenho);
canvas.addEventListener('touchend', pararDesenho);
canvas.addEventListener('touchcancel', pararDesenho);
canvas.addEventListener('touchmove', desenharTouch);

function iniciarDesenho(e) {
  desenhando = true;
  desenhar(e);
  e.preventDefault(); // Prevenir scroll em dispositivos touch
}

function pararDesenho() {
  desenhando = false;
  ctx.beginPath();
 
  // Salvar a assinatura como dados base64
  salvarAssinatura();
}

function desenhar(e) {
  if (!desenhando) return;
 
  const rect = canvas.getBoundingClientRect();
  const x = e.clientX - rect.left;
  const y = e.clientY - rect.top;
 
  ctx.lineTo(x, y);
  ctx.stroke();
  ctx.beginPath();
  ctx.moveTo(x, y);
}

function desenharTouch(e) {
  if (!desenhando) return;
 
  const rect = canvas.getBoundingClientRect();
  const touch = e.touches[0];
  const x = touch.clientX - rect.left;
  const y = touch.clientY - rect.top;
 
  ctx.lineTo(x, y);
  ctx.stroke();
  ctx.beginPath();
  ctx.moveTo(x, y);
 
  e.preventDefault(); // Prevenir scroll
}

function limparAssinatura() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.fillStyle = 'white';
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  document.getElementById('assinaturaData').value = '';
}

function salvarAssinatura() {
  document.getElementById('assinaturaData').value = canvas.toDataURL('image/png');
}

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/1b16676c-3d75-47b8-9e64-f290e03b3384n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages