问题
Hi I am developing a little app to move files around google drive using google Appmaker.
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);
}
The server script is:
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.
回答1:
The problem relies on hiding the server script. The official documentation says:
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.
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";
}
}
来源:https://stackoverflow.com/questions/52805378/google-script-run-xx-is-not-a-function-error