简体   繁体   English

Function 将 undefined 传递给 Google Apps 脚本中调用的 function

[英]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.我是 Google Apps 脚本的新手,有些功能似乎很难让我理解。 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. getFromZippedData function 应该将其返回值传递给whenReady function。 But, to do this, it calls another function ( getPic ), passing its first parameter to such function.但是,为此,它调用另一个 function ( getPic ),将其第一个参数传递给这样的 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 ).没有参数从getFromZippedFile传递给getPicLogger指责undefined的参数给picFile )。

Questions?问题?

How do I pass parameters from function to "google-side" function properly?如何正确地将参数从 function 传递到“google-side” function? The paramters used are primitive javascript types, as pointed in Google Apps Script reference.使用的参数是原始 javascript 类型,如 Google Apps 脚本参考中所述。 Is it needed even to "google-side" function?是否需要“谷歌端”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?如何在 HTML 文档中显示解压缩的 Blob(即 png 图像)的内容?

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.忽略它们(如果可能的话)并向我提供有关它们的建议。

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.) .您想知道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.您想将解压缩的 PNG 图像放在 HTML 端。

For this, how about this answer?为此,这个答案怎么样?

Answer 1:答案1:

I think that the reason of your issue is that slides[num] of Utilities.base64Encode( slides[num] );我认为您的问题的原因是Utilities.base64Encode( slides[num] ); slides[num] slides[num] is Blob .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.在这种情况下,从您的脚本中,它假定slides[num]是 blob。

Answer 2:答案 2:

I think that there is a modification point for putting the base64 encoded image in your HTML side.我认为将 base64 编码图像放在 HTML 端有一个修改点。 Please modify as follows.请进行如下修改。

From:从:

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

To:至:

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

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM