enter image description here您好,我正在尝试创建一个Google应用程序,该程序将在Google电子表格中搜索关键字Request(A列)并返回response(B列),脚本运行良好,但是只要我想知道一个最终用户调用的应用程序,它请求访问电子表格的权限,最终用户如何在没有允许权限的情况下访问该应用程序,我又该如何为该应用程序提供使用服务帐户读取代码中的电子表格的信息。 / p>
function ReadExcelFile()
{
var spreadsheetId = <MySpreadSheetID>;
var sheet = SpreadsheetApp.openById(spreadsheetId);
var SearchText='Hello';
var data = sheet.getRange('A:B').getValues();
for(var i in data){
if(i>0){
var row = data[i][0];
Logger.log('Searching for a KeyWord: '+SearchText+' From the SpreetSheet Value : '+row);
var SearchCount=SearchText.indexOf(row);
if(SearchCount>-1){
var row2=data[i][1];
Logger.log(SearchCount+' '+row2);
break;
}
}
}
}
Request | Response Hello | Hi Welcome!! Hey | Hey Welcome!!
输出将是.. 嗨,欢迎光临!
答案 0 :(得分:0)
我也遇到了这个问题,我想读取容器绑定的电子表格数据,并在最终用户向谷歌聊天机器人发送消息时将其作为消息回复,最后我通过这种方式解决了,它可能会有所帮助。 这是我的 appscript.json 代码
{
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"dependencies": {
"libraries": [
{
"userSymbol": "OAuth2",
"libraryId": "1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF",
"version": "26"
}
],
"enabledAdvancedServices": [
{
"userSymbol": "Sheets",
"version": "v4",
"serviceId": "sheets"
}
]
},
"chat": {
"addToSpaceFallbackMessage": "Thank you for adding me"
}
}
这里是 oauth、token 访问和查询 api 代码
var PRIVATE_KEY = 'your service account private key generated Google Cloud Project (GCP) console ';
var CLIENT_EMAIL = 'clien-email';
var ssId = 'Spreadsheet Id which you want to read or write data from end user';
/**
* Configures the Chatbot service.
*/
function getChatbotService() {
return OAuth2.createService("sathibhai-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");
}
/**
* 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");
}
function readSheet(){
var service = getSpreasheetService();
var range = 'Sheet1!A1:D';
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][1] + ":" + values[row][2] + ":" + values[row][3]); //you can use these data as your requirements
}
注意: