Daniel Bandeira
Daniel Bandeira

Reputation: 378

Function passes undefined to called function in Google Apps Script

I am new in Google Apps Script and some features seems to really hard to get in my understanding. In my code: (look carefully to script element near the end of the body)

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>
//some code ... 
    </style></head>
  <body>
  <div><h1>Cabeçalho escolar</h1></div>
  <div class="palco">
    <img id="dataShow"/>
 </div>
 
  <script>
    function whenReady(dataImage) //shows the content png image 
    {
       document.getElementById("dataShow").scr = "image/png;base64;"+dataImage; //I dont know if this will work; dataImage should be a base64Encode output from a blob png (that was zipped together with others files) 
//the second parameter is a index to pick up the png file.
    }
       google.script.run.withSuccessHandler(whenReady).getFromZippedData("example.zip",0) 
    
    </script>
</body>
</html>

The getFromZippedData function should pass its return value to whenReady function. But, to do this, it calls another function (getPic), passing its first parameter to such function. See bellow:

function getPic(fileName) {

var pasta = 'DirectoryName'; 
var arquivos = DriveApp.getRootFolder().getFoldersByName(pasta).next().getFiles(); // yes, the first match... for while, assume that exists such folder
  while( arquivos.hasNext() )
  {
    var candidato = arquivos.next();
    if( candidato.getName() === fileName )
    {
      return Utilities.unzip( candidato.getBlob() );
    }
  }
  Logger.log("Erro ao procurar o arquivo \""+fileName+"\" na função getPic") // aula 
  return null;
}

function getFromZippedData( picFile , num )
{

  var slides = getPic( picFile ); // <-- THIS IS THE POINT (main)
   if( 0 < num )
     num = 0;
   if( num >= slides.length )
     num = slides.length - 1;
   return Utilities.base64Encode( slides[num] );
}

Problem

No parameter is passed from getFromZippedFile to getPic (Logger accuses undefined parameter to picFile).

Questions?

How do I pass parameters from function to "google-side" function properly? The paramters used are primitive javascript types, as pointed in Google Apps Script reference. Is it needed even to "google-side" function?

Side-Question (if doesn't imply 'duplicate')

How do I display the content of the unzipped Blob (that is a png image) in a HTML document?

Sorry if I have made a stupid question, but I have spend unproductive hours on this. This minimal code was revised, but something could be wrong. I am really tired right now. It's possible some mistakes getting place. Ignore them (if possible) and advise me about them.

Upvotes: 2

Views: 671

Answers (1)

Tanaike
Tanaike

Reputation: 201388

I believe your question as follows.

  1. You want to know the reason of the issue of No parameter is pass from getFromZippedFile to getPic (Logger accuses undefined paramter to picFile.).
  2. You want to put the unzipped PNG image on HTML side.

For this, how about this answer?

Answer 1:

I think that the reason of your issue is that slides[num] of Utilities.base64Encode( slides[num] ); is Blob. In this case, please use the byte array. So please modify your script as follows.

From:

return Utilities.base64Encode( slides[num] );

To:

return Utilities.base64Encode(slides[num].getBytes());
  • In this case, from your script, it supposes that slides[num] is the blob.

Answer 2:

I think that there is a modification point for putting the base64 encoded image in your HTML side. Please modify as follows.

From:

document.getElementById("dataShow").scr = "image/png;base64;"+dataImage;

To:

document.getElementById("dataShow").src = "data:image/png;base64,"+dataImage;

References:

Upvotes: 2

Related Questions