google.script.run XX不是函数错误 [英] google.script.run XX is not a function error

查看:87
本文介绍了google.script.run XX不是函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小应用程序,以便使用Google Appmaker在Google驱动器中移动文件.

Hi I am developing a little app to move files around google drive using google Appmaker.

我有可以选择文件和目标目录的代码.问题是调用服务器函数来运行DriveApp函数,如下所示:

I have the code working to select the file and the destination directory all fine. The problem is to call the server function to run DriveApp functions as follows:

function onClickbtnMove(widget, event){
   var props = widget.root.properties;     
   fileids=props.FileIdList;
 //fileids is a list object of fileIDs, in the following text i removed the loop and just try with one fileID
   var i=0;

   google.script.run
    .withSuccessHandler (function (result) {
      console.log (result);
      })
    .withFailureHandler (function (error) {
     console.log (error);
      })
    .moveFiles_(fileids[i], props.FolderDestinationId);
      } 

服务器脚本为:

function moveFiles_(sourceFileId, targetFolderId) {

   var file = DriveApp.getFileById(sourceFileId);
  // file.getParents().next().removeFile(file); // removed until i get it working!!
  DriveApp.getFolderById(targetFolderId).addFile(file);
  return "1";
 }

我确定确实有一些明显的东西,但是我得到了:

i am sure there is something totally obvious but i am getting:

 google.script.run.withSuccessHandler(...)
.withFailureHandler(...).moveFiles_ is not a function`

任何指导都非常欢迎.预先感谢.

any guidance very welcome. thanks in advance.

推荐答案

问题取决于隐藏服务器脚本. 官方文档说:

请务必注意,即使您未在UI中公开您在服务器脚本中定义的任何功能,该功能对应用程序的所有用户都是开放的.如果要编写只能从其他服务器脚本调用的实用程序功能,则必须在名称后加上下划线.

It is important to note any function you define in a server script is open to all users of your application, even if you do not expose it in the UI. If you want to write a utility function that can only be called from other server scripts, you must append an underscore to the name.

因此,通过在函数名称后添加下划线,可以将其从客户端隐藏起来,因此会出现该错误.为了使用google.script.run调用该函数,必须除去下划线,即,将函数moveFiles_(sourceFileId, targetFolderId)更改为moveFiles(sourceFileId, targetFolderId).

So by appending an underscore to the function name, you are hiding it from the client, hence you are getting that error. In order to call the function using google.script.run, you must get rid of the underscore, i.e., the function moveFiles_(sourceFileId, targetFolderId) should be changed to moveFiles(sourceFileId, targetFolderId).

如果您感觉要向客户端公开敏感信息,那么在这种情况下,重要的是通过实现自己的方法来保护脚本.例如以下内容:

If you feel you are exposing sensitive information to the client, then in this case, it's important to secure your scripts by implementing your own method. Take for example the following:

function moveFiles(sourceFileId, targetFolderId, role) {    
   if(role === "Manager" || role === "Admin"){
       var file = DriveApp.getFileById(sourceFileId);
       DriveApp.getFolderById(targetFolderId).addFile(file);
       return "1";
   }
 }

这篇关于google.script.run XX不是函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆