简体   繁体   English

如何从Google表格获取数据以在已部署的Web应用程序中使用

[英]How to get data from Google Sheet to use in deployed web app

I am using Google App scripts and am having trouble finding an answer to this fairly simple task: how can I get the data from a Google Sheet in my code.gs and then use it in a published web app (for some visualization w/ D3)? 我正在使用Google App脚本,却找不到这个相当简单的任务的答案:如何从我的code.gs中的Google表格中获取数据,然后在已发布的Web应用程序中使用它(用于某些具有D3的可视化效果) )?

I would like to get all columns and rows from the spreadsheet, then use those in the data. 我想从电子表格中获取所有列和行,然后在数据中使用它们。 Preferably they would be in JSON for ease of use, but I can't find the specifics on how to grab all (or any) of the data from a Sheet in my code.gs and then use it in the index.html. 为了便于使用,最好使用JSON格式,但是我找不到在我的code.gs中如何从Sheet中获取所有(或任何)数据,然后在index.html中使用它们的细节。

So far I have something like this: 到目前为止,我有这样的事情:

code.gs 代码

function getData(){
 var sheet = SpreadsheetApp.openById("1NCNsdEGAHXWhZMEHnOflsIqElKdQSQxy83iM5vUIa9k");

 //get desired range

 return sheet;
}

index.html index.html

In the <script> in index.html: 在index.html中的<script>中:

 google.script.run.getData();

But am otherwise at a loss. 但是我不知所措。 Thanks for any help. 谢谢你的帮助。

Getting the data is fairly easy. 获取数据非常容易。 However this has to be properly done on both server side and client side. 但是,这必须在服务器端和客户端都正确完成。

For code.gs you will need to return actual data, but currently return sheet just returns the sheet object. 对于code.gs,您将需要返回实际数据,但是当前return sheet只返回sheet对象。 What you need to do instead is 您需要做的是

var vals = sheet.getDataRange().getValues()

which now gets you a 2D array of your sheet values. 现在,您将获得一个2D图纸值数组。 Now keep in mind that sheet has to be a sheet object not a spreadhseet object . 现在请记住,工作sheet必须是工作sheet object而不是spreadhseet object So if your spreadsheet has many sheets, make sure you get the correct one with something like 因此,如果您的电子表格有很多工作表,请确保您获得正确的工作表,例如

var sheet = SpreadsheetApp.openById("1NCNsdEGAHXWhZMEHnOflsIqElKdQSQxy83iM5vUIa9k").getSheets()[0]; 

or 要么

var sheet = SpreadsheetApp.openById("1NCNsdEGAHXWhZMEHnOflsIqElKdQSQxy83iM5vUIa9k").getSheetByName('Sheet1');

Now, since you want to grab the data as a JSON, all you really have to do is 现在,由于您要以JSON形式获取数据,因此您真正要做的就是

vals = JSON.stringify(vals)

and now you have a JSON string of values. 现在您有了一个JSON值字符串。 However that is just half of it, because your index.html that you wrote will not make use of the data as it calls the server side function, but does not listen for a response. 但是,这仅仅是其中的一半,因为您编写的index.html不会使用数据,因为它调用服务器端函数,但不会监听响应。 In order to do that, you will need to use a success handler as written here . 为此,您将需要使用此处编写的成功处理程序。 Essentially you will have to call a function in index.html that will make use of the returned vals variable. 本质上,您将必须在index.html中调用一个函数,该函数将使用返回的vals变量。 So something like 所以像

google.script.run.withSuccessHandler(useData).getData();

function useData(data) {
   data = JSON.parse(data)
   //now you have a 2D array in your index.html and you can use it as you wish within these client side scripts
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从部署为网络应用的Google应用脚本中打开电子表格 - How to open spreadsheet from Google app script deployed as web app 如何让用户从Google Apps脚本网络应用访问Google表格中的数据? - How to let users access data from a Google Sheet from a Google Apps Script web app? 如何根据单元格值将 Google 表格中的数据显示到 Google Web 应用程序? - How to show data from Google Sheet To Google Web App based on A Cell Value? 从 AWS Lambda 将数据发布到 Google Sheet web 应用程序 - POST data to Google Sheet web app from AWS Lambda 如何使用Google App脚本从Firebase获取数据并将其保存到Google表格中? - How to get data from Firebase and save into Google Sheet using Google App Script? 使用脚本和网络应用将数据从Google表格的一张复制到另一张 - Copy data from one sheet of Google Sheet to another with script and web-app 获取和使用Google文档中另一个工作表中的用户数据 - Get and use user data from another sheet in Google Docs 通过Google表格创建网络应用 - Creating a web app from Google Sheet 从已部署的Web应用程序中获取值 - Google App Script - Grab value from class in deployed web app - Google App Script Google Sheet &amp; App Script:如何从链接预览中获取图像? - Google Sheet & App Script : how to get image from a link preview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM