如何使用javascript在google工作表中创建图表?
我正在使用javascript创建包含用户数据的GoogleSheets文档。文档保存在用户的驱动器上。 我不知道如何用我插入的数据制作图表。我使用的是带有GoogleSheets API的香草javascript。 它可能看起来像这样:如何使用javascript在google工作表中创建图表?,javascript,google-sheets,google-sheets-api,Javascript,Google Sheets,Google Sheets Api,我正在使用javascript创建包含用户数据的GoogleSheets文档。文档保存在用户的驱动器上。 我不知道如何用我插入的数据制作图表。我使用的是带有GoogleSheets API的香草javascript。 它可能看起来像这样: function createGraph() { gapi.client.sheets.graph .create({ properties: { type(?): 'Pie'
function createGraph() {
gapi.client.sheets.graph
.create({
properties: {
type(?): 'Pie'
spreadsheetid: //some id
range: 'A1:A10'
},
})
}
编辑:要指定,我希望将图形插入到我创建的sheets文档中,而不是插入到网站中。参考此示例
var数据;
var图;
//加载可视化API和piechart包。
load(“当前”{packages:[“corechart”]});
//将回调设置为在加载Google Visualization API时运行。
google.charts.setOnLoadCallback(drawChart);
//创建并填充数据表的回调,
//实例化饼图,传入数据并
//画它。
函数绘图图(){
//创建我们的数据表。
data=new google.visualization.DataTable();
data.addColumn(“字符串”、“顶部”);
data.addColumn(“编号”、“切片”);
data.addRows([
[“蘑菇”,3],
[“洋葱”,1],
[“橄榄”,1],
[“西葫芦”,1],
[“意大利香肠”,2]
]);
//设置图表选项
变量选项={
标题:“我昨晚吃了多少比萨饼”,
宽度:400,
身高:300
};
//实例化并绘制图表,传入一些选项。
chart=新的google.visualization.PieChart(
document.getElementById(“图表分区”)
);
图表绘制(数据、选项);
}
如果要将图表添加到电子表格中,可以使用Sheets API作为方法的一部分
代码段:
从广义上讲,您的请求如下所示(请查看下面的参考以详细构建请求主体):
在浏览器和移动设备中呈现图表:
如果您只想在浏览器中呈现图表,而不想将其添加到电子表格中,则可以使用(例如,请参见)
参考:
- 我解决了。谢谢你的帮助!这对我有用
function createGraphv2(spreadsheetIdGraph, endIndex) {
var params = {
// The spreadsheet to apply the updates to.
spreadsheetId: spreadsheetIdGraph, // TODO: Update placeholder value.
};
var batchUpdateSpreadsheetRequestBody = {
// A list of updates to apply to the spreadsheet.
// Requests will be applied in the order they are specified.
// If any request is not valid, no requests will be applied.
requests: [
{
addChart: {
chart: {
spec: {
title: 'Rapport',
basicChart: {
chartType: 'COLUMN',
legendPosition: 'BOTTOM_LEGEND',
axis: [
//X-AXIS
{
position: "BOTTOM_AXIS",
title: "FORBRUK"
},
//Y-AXIS
{
position: "LEFT_AXIS",
title: "TID"
}
],
series: [
{
series: {
sourceRange: {
sources: [
{
sheetId: 0,
startRowIndex: 0,
endRowIndex: endIndex,
startColumnIndex: 5,
endColumnIndex: 6,
},
],
},
},
targetAxis: "LEFT_AXIS"
}
]
}
},
position : {
newSheet : 'True'
}
},
}
}
],
// TODO: Add desired properties to the request body.
};
var request = gapi.client.sheets.spreadsheets.batchUpdate(
params,
batchUpdateSpreadsheetRequestBody
);
request.then(
function (response) {
// TODO: Change code below to process the `response` object:
console.log(response.result);
},
function (reason) {
console.error("error: " + reason.result.error.message);
}
);
}
function createGraphv2(spreadsheetIdGraph, endIndex) {
var params = {
// The spreadsheet to apply the updates to.
spreadsheetId: spreadsheetIdGraph, // TODO: Update placeholder value.
};
var batchUpdateSpreadsheetRequestBody = {
// A list of updates to apply to the spreadsheet.
// Requests will be applied in the order they are specified.
// If any request is not valid, no requests will be applied.
requests: [
{
addChart: {
chart: {
spec: {
title: 'Rapport',
basicChart: {
chartType: 'COLUMN',
legendPosition: 'BOTTOM_LEGEND',
axis: [
//X-AXIS
{
position: "BOTTOM_AXIS",
title: "FORBRUK"
},
//Y-AXIS
{
position: "LEFT_AXIS",
title: "TID"
}
],
series: [
{
series: {
sourceRange: {
sources: [
{
sheetId: 0,
startRowIndex: 0,
endRowIndex: endIndex,
startColumnIndex: 5,
endColumnIndex: 6,
},
],
},
},
targetAxis: "LEFT_AXIS"
}
]
}
},
position : {
newSheet : 'True'
}
},
}
}
],
// TODO: Add desired properties to the request body.
};
var request = gapi.client.sheets.spreadsheets.batchUpdate(
params,
batchUpdateSpreadsheetRequestBody
);
request.then(
function (response) {
// TODO: Change code below to process the `response` object:
console.log(response.result);
},
function (reason) {
console.error("error: " + reason.result.error.message);
}
);
}