从清单部署的容器绑定应用程序脚本可以允许其他用户从容器电子表格中读取数据,而无需共享文件



我正在通过容器(电子表格(绑定的应用程序开发聊天机器人。从清单中为可能的内部组织成员部署脚本,成员只需键入并通过谷歌聊天发送即可请求存储在表单上的信息。

如果我共享电子表格,成员可以通过聊天机器人自动获取信息,否则机器人不会回复消息。

当我共享文件时,所有成员驱动器也会显示碎片副本。我想让用户在不共享Container电子表格的情况下阅读这些内容。

我必须将哪些代码添加到我的脚本中,以便用户在聊天期间获得读取我的文件的权限。这可能吗?

我的舱单代码如下

{
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Sheets",
"serviceId": "sheets",
"version": "v4"
}
],
"libraries": [
{
"userSymbol": "OAuth2",
"libraryId": "1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF",
"version": "40"
}
]
},

"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"chat": {
"addToSpaceFallbackMessage": "Hi ! Thanks for adding me. Type help to get more.."
}
}

和用户oauth2。。

/**
* Configures the Chatbot service.
*/
function getChatbotService() {
return OAuth2.createService("chat-sheet-bot")
// Set the endpoint URL.
.setTokenUrl("https://accounts.google.com/o/oauth2/token")
// Set the private key and issuer.
.setPrivateKey(PRIVATE_KEY)
.setIssuer(CLIENT_EMAIL)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getScriptProperties())
// Set the scope.
.setScope("https://www.googleapis.com/auth/chat.bot https://www.googleapis.com/auth/spreadsheets");
}

最后我解决了如下问题:

  1. 添加了get服务方法,电子表格范围为
/**
* Configures the spreadsheet service.
*/
function getSpreasheetService() {
return OAuth2.createService("spreadsheet")
// Set the endpoint URL.
.setTokenUrl("https://accounts.google.com/o/oauth2/token")
// Set the private key and issuer.
.setPrivateKey(PRIVATE_KEY)
.setIssuer(CLIENT_EMAIL)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getScriptProperties())
// Set the scope.
.setScope("https://www.googleapis.com/auth/spreadsheets");
}
  1. 添加了方法read sheet,由服务帐户使用电子表格api从电子表格中获取数据
function readSheet(){
var service = getSpreasheetService();
var ssID = 'your Spreadsheet Id' 
var range = 'Sheet1!A3:E';
var url = 'https://sheets.googleapis.com/v4/spreadsheets/' + ssId +'/values/' + range;
var response = UrlFetchApp.fetch(url, { headers: {Authorization: 'Bearer ' + service.getAccessToken() } }); 
var rep = JSON.parse(response.getContentText());
var values =  rep.values;
for(row in values)
Logger.log(values[row][0] + ":" + values[row][2] + ":" + values[row][3] + ":" + values[row][4]);
//now i can use these data to reply back to end user as message
}
  1. 我将电子表格共享给了服务帐户,看起来像";your-service-account@sys................iam.gserviceaccount.com">

  2. 我在API上的Google Cloud Project控制台中启用了Sheet API;服务

哦!它非常有魅力。

最新更新