Google Picker-将文件ID返回到我的Google脚本 [英] Google Picker - Return the File ID to my Google Script
问题描述
我有一个相当基本的电子表格,该电子表格使用一些Google脚本来完成各种任务.我试图为最终用户清理界面,并决定实施Google Picker.最初,用户必须手动将CSV导入电子表格.此处的新目标是通过Google Picker选择CSV,然后将其上传,导入,删除.我已经拥有所有导入和删除它的代码.我刚刚编写了选择器的代码,它似乎工作正常.但是,我想我只是缺少了一些小东西,如何将文件ID从Picker.html传递回我的Google脚本,以便继续执行该过程?
I have a fairly basic spreadsheet that uses some Google Scripts to accomplish various tasks. I was trying to cleanup the interface for the end user, and decided to implement the Google Picker. Originally the user had to manually import a CSV into the spreadsheet. The new goal here is to select the CSV via the Google Picker, upload it, import it, then delete it. I already have all the code working to import it and delete it. I just worked up the code for the picker, and it seems to work fine. However, and I think I'm just missing something small, how do I pass the File ID back from the Picker.html to my Google Scripts in order to continue my process?
如果有帮助,我现在正在使用Google文档中提供的基本回调.我假设这是进行更改的地方.只是不确定该怎么办.
If it helps, I'm using the basic callback provided in the Google documentation right now. I'm assuming this is where the change will be made. Just not sure what to do.
function pickerCallback(data) {
var action = data[google.picker.Response.ACTION];
if (action == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
var id = doc[google.picker.Document.ID];
var url = doc[google.picker.Document.URL];
var title = doc[google.picker.Document.NAME];
document.getElementById('result').innerHTML =
'<b>You chose:</b><br>Name: <a href="' + url + '">' + title + '</a><br>ID: ' + id;
} else if (action == google.picker.Action.CANCEL) {
document.getElementById('result').innerHTML = 'Picker canceled.';
}
}
推荐答案
这可能应该起作用:
在您的pickerCallback(data)函数中:
In your pickerCallback(data) function:
if (data.action == google.picker.Action.PICKED) {
var fileId = data.docs[0].id;
google.script.run
.withSuccessHandler(useData) // this will call the google apps script function in your Code.gs file
.doSomething(fileId); // this is a function in your JavaScript section where you will do something with the code you got from your apps script function
}
function useData(data) {
// do something with the data
}
在Code.gs中,创建一个函数来处理选择器的输入:
In Code.gs, create a function to handle the input from the picker:
function doSomething(fileId) {
// do an operation in Drive with the fileId
var file = DriveApp.getFileById(fileId);
var fileName = file.getName();
return fileName;
}
这篇关于Google Picker-将文件ID返回到我的Google脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!