我以前使用过tabletop.js 1,非常棒!你可以做任何你想做的事。
我看到的唯一问题是,您需要将电子表格发布到web上,如果您正在处理敏感数据,这当然是非常危险的。
我现在需要在一个敏感数据的项目中使用它,所以我希望有人能指导我如何使用没有发布到网络上的电子表格。
我已经搜索了很长一段时间,但没有成功,但似乎tabletop.js确实支持私有表(以下是添加此选项2的拉请求)。
事实上,看看他们所包含的文档
authkey
authkey is the authorization key for private sheet support.问:我应该如何使用authkey?有人能给我举个例子让我试试吗?
提前感谢!
发布于 2020-07-29 02:32:26
这个答案怎么样?
问题和解决办法:
在"tabletop.js“处,从请求的端点(https://spreadsheets.google.com/feeds/list/###/###/private/values?alt=json)来看,"tabletop.js”似乎使用了Sheets v3。当使用authkey时,将oauth_token=authkey添加到查询参数中。在这种情况下,不幸的是,似乎无法用它访问私有电子表格。从这种情况来看,不幸的是,我认为在现阶段,"tabletop.js“可能无法使用私有电子表格。但我不确定这是否会在未来的更新中解决。当然,似乎可以使用这个库访问网络发布的电子表格。
因此,在这个答案中,我想提出从电子表格中检索作为JSON对象的值的解决办法。
模式1:
在这种模式中,使用了Google脚本。使用Google脚本,可以方便地访问私有电子表格。
示例脚本:
使用此脚本时,请将其复制并粘贴到脚本编辑器并运行函数myFunction。
function myFunction() {
const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const sheetName = "Sheet1"; // Please set the sheet name.
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
const values = sheet.getDataRange().getValues();
const header = values.shift();
const object = values.map(r => r.reduce((o, c, j) => Object.assign(o, {[header[j]]: c}), {}));
console.log(object) // Here, you can see the JSON object from Spreadsheet.
}模式2:
在这种模式中,使用由Google脚本创建的Web应用程序。当使用Web时,可以轻松地访问私有电子表格。因为Web是用Google脚本创建的。在这种情况下,您可以通过登录到Google帐户从外部访问Web应用程序。并且,可以在HTML和Javascript中检索JSON对象。
用法:
请执行以下流程。
1.创建新的Google脚本项目。
Web的示例脚本是Google脚本。因此,请创建一个项目的谷歌应用程序脚本。为了使用文档服务,在本例中,使用wrapper作为包装器。
如果您想直接创建它,请访问https://script.new/。在这种情况下,如果没有登录Google,则打开登录屏幕。所以请登陆谷歌。这样,Google脚本的脚本编辑器就打开了。
2.编写脚本。
请将以下脚本(Google脚本)复制并粘贴到脚本编辑器中。此脚本用于Web应用程序。
Google脚本端:Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile("index");
}
function getObjectFromSpreadsheet(spreadsheetId, sheetName) {
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
const values = sheet.getDataRange().getValues();
const header = values.shift();
const object = values.map(r => r.reduce((o, c, j) => Object.assign(o, {[header[j]]: c}), {}));
return object;
}HTML和Javascript端:index.html
<script>
const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const sheetName = "Sheet1"; // Please set the sheet name.
google.script.run.withSuccessHandler(sample).getObjectFromSpreadsheet(spreadsheetId, sheetName);
function sample(object) {
console.log(object);
}
</script>spreadsheetId和sheetName从Javascript端提供给Google脚本端。从这种情况来看,在这种情况下,getObjectFromSpreadsheet可能代替"tabletop.js“。3.部署Web应用程序。
https://script.google.com/macros/s/###/exec。4.使用Web应用程序运行该函数。
您可以按以下方式测试上面的脚本。
https://script.google.com/macros/s/###/exec )的URL。这样,您就可以在控制台上看到检索到的JSON对象。
注意:
参考文献:
https://stackoverflow.com/questions/63135606
复制相似问题