google.script.run XX不是函数错误

时间:2018-10-14 17:43:15

标签: google-app-maker

嗨,我正在开发一个小应用程序,用于使用Google Appmaker在Google驱动器中移动文件。

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

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";
 }

我确信有些东西完全显而易见,但是我得到了:

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

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

1 个答案:

答案 0 :(得分:2)

问题取决于隐藏服务器脚本official documentation说:

  

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

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

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

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