0
votes

I've created a web application that ask users to sign manually a spreasheet document. In order to do it I wrote the code below :

function saveSign(url){ // url = canvas.toDataURL("image/png");
  const email = Session.getActiveUser().getEmail(); 
  const base64 = url.split(",")[1];
  const decoded = Utilities.base64Decode(base64);
  const blob = Utilities.newBlob(decoded, MimeType.PNG, email);
  const file = DriveApp.createFile(blob);
  return true;
};

This code will create a file with the user email title in each users personal drive who use this web app.

All is well, but when I try to insert the picture just created or already created and stored in the user personal drive on the spreadsheet with the code below, an error occur.

const ssDA = SpreadsheetApp.openById("idSpreadsheet");
const wsDA = ssDA.getSheetByName("sheetName");
let uBlob;
const files = DriveApp.getFiles();
while (files.hasNext()) {
  let file = files.next();
  let fFileName = file.getName();
  if(fFileName === ufileName){
    uBlob = file.getBlob();
  }
}
wsDA.insertImage(uBlob, 12, 32); // Error in this line

Console log Error :

Exception: Argument non valide : url

When I try to insert an image stored in my Drive all is well but when a colleague try to insert an image stored in his personal Drive the code above doen't work.

Indeed the while loop (var files) iterate in my personal folder and not in the user personal folder.

See below the web app deployement informations :

Execute the app as: User accessing the web app

Could you please help me ?

1
Can I ask you about your question? 1. Where does the error of Exception: Argument non valide : url occur in your script? 2. About All is well, can I ask you about the detail of it? 3. Can I ask you about the scopes using in your script?Tanaike
@Tanaike: The error occur on the line wsDA.insertImage(uBlob, 12, 32);. When I said all is well, I wanted to say that the function saveSign work as well, it mean that users who use the web App. can save the canvas drawing in their own personal drive. The second code with the while loop is triggered after a form submit. The script scopes are listed below : "https://www.googleapis.com/auth/spreadsheets", "userinfo.email", "forms", "script.scriptapp", "script.send_mail", "script.external_request", "drive" Thks in advance !Michael
Thank you for replying. I noticed that an answer has already been posted. In this case, I would like to respect the existing answer. It will resolve your issue.Tanaike

1 Answers

0
votes

I don't know if you have your web app set up (permissions, if script is linked to a spreadsheet, standalone, if each user has to have their own spreadsheet) but here is an example similar to yours.

Instructions:

I have my primary account and a friend's account.

In primary account:

  1. Created new script project.
  2. Created new spreadsheet.
  3. Made an image called "doggo.jpg" in my Drive.
  4. Made a few functions:
function insertImg (fileName) {
  const ssDA = SpreadsheetApp.openById("{spreadsheetid}"); // use your new spreadsheet id
  const wsDA = ssDA.getSheetByName("{sheetname}");
  const ufileName = fileName
  
  const files = DriveApp.getFiles();
  while (files.hasNext()) {
    let file = files.next();
    let fFileName = file.getName();
    if(fFileName === ufileName){
      uBlob = file.getBlob();
    }
  }
  wsDA.insertImage(uBlob, 10, 10);
}

function testInsert() { // THIS IS THE TEST FUNCTION
  insertImg("doggo.jpg")
}

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile("page"); // next we will make this page
}
  1. Made a html file "page.html" in the script project with a button and a addEventListener to run the test function, in this example I am not including the boilerplate html (Doctype, head etc):
<body>
  <h1>Hello</h1>
  <button id="btn">Run Function</button>
  
  <script>
    document.getElementById("btn").addEventListener("click", doStuff);
    function doStuff(){
      google.script.run.insertImg("doggo.jpg"); // here is the running of the test function.
    };
  </script>
</body>

  1. Ran the tests and ensured that I had given all the permissions to the script.
  2. Published web app, Execute the app as: User accessing the web app, Who has access to the app: Anyone
  3. Opened web app, pressed the button and it inserted the image to the spreadsheet. Up to here, same as you I think.
  4. Shared the Google Sheet with friend's account. - very important.

Now friend logged into their account:

  1. Got another image with same name "doggo.jpg" in friend's account's Drive.
  2. Opened the URL of the web app and gave the necessary permissions.
  3. Tested the button, and it worked!

Reference:

https://developers.google.com/apps-script/guides/web