[英]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.