การค้นหาข้อมูล
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หน้านี้จะอธิบายวิธีส่งคำค้นหาไปยังแหล่งข้อมูลที่รองรับโปรโตคอลแหล่งข้อมูลของเครื่องมือแผนภูมิ
เนื้อหา
ภาพรวม
แหล่งข้อมูลคือบริการบนเว็บที่รองรับโปรโตคอลแหล่งข้อมูลของเครื่องมือแผนภูมิ คุณสามารถส่งการค้นหา SQL ไปยังแหล่งข้อมูล แล้วคุณจะได้รับ DataTable พร้อมข้อมูลที่เหมาะสมในการตอบสนอง ตัวอย่างบางส่วนของแหล่งข้อมูล ได้แก่ Google สเปรดชีตและ SalesForce
การส่งคำขอ
วิธีส่งคำขอ
-
สร้างอินสแตนซ์ออบเจ็กต์การค้นหาด้วย URL ของแหล่งข้อมูล URL ควรระบุว่ามีการขอข้อมูลใดในรูปแบบไวยากรณ์ที่แหล่งข้อมูลนั้นเข้าใจ
-
(ไม่บังคับ) ระบุตัวเลือกคำขอ เช่น วิธีการส่งเป็นพารามิเตอร์ที่ 2 ที่ไม่บังคับในเครื่องมือสร้างออบเจ็กต์
Query
(ดูรายละเอียดในพารามิเตอร์ opt_options
ของตัวสร้างการค้นหา)
-
(ไม่บังคับ) เพิ่มสตริงภาษาของการค้นหาเพื่อจัดเรียงหรือกรองผลลัพธ์ แล้วส่งคำขอ ไม่จำเป็นต้องใช้แหล่งข้อมูลเพื่อรองรับภาษาสำหรับคำค้นหาแหล่งข้อมูลของเครื่องมือแผนภูมิ หากแหล่งข้อมูลไม่รองรับภาษาที่ใช้ค้นหา ก็จะไม่สนใจสตริงการค้นหา SQL แต่ยังคงแสดงผล
DataTable
ภาษาที่ใช้ค้นหาเป็นตัวแปรของภาษา SQL โปรดอ่านไวยากรณ์ภาษาของคำค้นหาที่นี่ฉบับเต็ม
-
ส่งคำขอโดยระบุเครื่องจัดการ Callback ที่จะเรียกใช้เมื่อได้รับการตอบกลับ โปรดดูรายละเอียดที่หัวข้อถัดไป
ต่อไปนี้คือตัวอย่างการส่งคำขอข้อมูลในช่วงเซลล์ของสเปรดชีต Google หากต้องการดูวิธีรับ URL สำหรับสเปรดชีตของ Google โปรดดูที่นี่
function initialize() {
var opts = {sendMethod: 'auto'};
// Replace the data source URL on next line with your data source URL.
var query = new google.visualization.Query('http://spreadsheets.google.com?key=123AB&...', opts);
// Optional request to return only column C and the sum of column B, grouped by C members.
query.setQuery('select C, sum(B) group by C');
// Send the query with a callback function.
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
// Called when the query response is returned.
...
}
หากส่งคำถามจากภายใน Apps Script โปรดใช้โหมด IFRAME
การประมวลผลคำตอบ
ระบบจะเรียกฟังก์ชันตัวแฮนเดิลการตอบกลับของคุณเมื่อคำขอแสดงผล พารามิเตอร์ที่ส่งผ่านในฟังก์ชันเครื่องจัดการการตอบกลับเป็นประเภท google.visualization.QueryResponse
หากคำขอประสบความสำเร็จ การตอบกลับจะมีตารางข้อมูล (คลาส google.visualization.DataTable
) หากคำขอล้มเหลว การตอบกลับจะมีข้อมูลเกี่ยวกับข้อผิดพลาด ไม่ใช่ DataTable
เครื่องจัดการการตอบสนองควรดำเนินการดังนี้
-
ตรวจสอบว่าคำขอดำเนินการสำเร็จหรือล้มเหลวโดยโทรหา
response.isError()
คุณไม่ควรแสดงข้อความแสดงข้อผิดพลาดต่อผู้ใช้ ไลบรารีการแสดงภาพจะแสดงข้อความแสดงข้อผิดพลาดในคอนเทนเนอร์ <div>
อย่างไรก็ตาม หากคุณต้องการจัดการข้อผิดพลาดด้วยตนเอง คุณสามารถใช้คลาส
goog.visualization.errors
เพื่อแสดงข้อความที่กำหนดเองได้ (ดูตัวอย่าง Wrapper การค้นหาสำหรับตัวอย่างการจัดการข้อผิดพลาดที่กำหนดเอง)
-
หากคำขอประสบความสำเร็จ การตอบกลับจะมี
DataTable
ที่คุณดึงข้อมูลได้โดยการเรียกใช้ getDataTable()
ส่งต่อไปที่แผนภูมิของคุณ
โค้ดต่อไปนี้แสดงการจัดการคำขอวาดแผนภูมิวงกลมก่อนหน้านี้:
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240, is3D: true});
}
การอ่านไฟล์ CSV
หากต้องการสร้างแผนภูมิจากข้อมูล CSV (ค่าที่คั่นด้วยคอมมา) คุณมี 2 ตัวเลือก แปลงข้อมูล CSV เป็นรูปแบบตารางข้อมูลของ Google Charts ด้วยตนเอง หรือวางไฟล์ CSV ลงในเว็บเซิร์ฟเวอร์ที่แสดงแผนภูมิ แล้วค้นหาโดยใช้เทคนิคในหน้านี้
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2024-07-10 UTC
[null,null,["อัปเดตล่าสุด 2024-07-10 UTC"],[[["\u003cp\u003eThis page explains how to send a SQL query to a Datasource, a web service supporting the Chart Tools Datasource protocol, to receive a DataTable with the requested data.\u003c/p\u003e\n"],["\u003cp\u003eYou can use a Query object to send a request with optional parameters for sending method and a query language string for filtering data, receiving a response handled by a callback function.\u003c/p\u003e\n"],["\u003cp\u003eThe response handler checks for errors and, if successful, retrieves the DataTable from the QueryResponse for use in visualizations like charts.\u003c/p\u003e\n"],["\u003cp\u003eCSV data can be either manually converted to Google Charts datatable format or placed on a web server and queried using the techniques described on the page.\u003c/p\u003e\n"],["\u003cp\u003eMore detailed information on query language syntax, the Query class, and the QueryResponse class can be found via provided links.\u003c/p\u003e\n"]]],[],null,["# Data Queries\n\nThis page describes how to send a query to a data source that supports the Chart Tools Datasource\nprotocol.\n\nContents\n--------\n\n1. [Overview](#overview)\n2. [Sending a request](#Sending_a_Query)\n3. [Processing the response](#Processing_the_Query_Response)\n4. [Reading CSV files](#csv)\n5. [More information](#moreinfo)\n\nOverview\n--------\n\n\nA Datasource is a web service that supports the Chart Tools Datasource protocol. You can send a\nSQL query to a Datasource, and in response you will receive a DataTable populated with the\nappropriate information. Some examples of Datasources include\n[Google Spreadsheets](/chart/interactive/docs/spreadsheets) and SalesForce.\n\nSending a request\n-----------------\n\n**To send a request:**\n\n1. Instantiate a [Query](/chart/interactive/docs/reference#Query) object with the URL of your Datasource. The URL should indicate what data is being requested, in a syntax understood by that data source.\n2. Optionally specify request options such as sending method as an optional second parameter in the `Query` object constructor (see the Query constructor's [`opt_options`](/chart/interactive/docs/reference#Query) parameter for details):\n3. Optionally add a [query language string](/chart/interactive/docs/querylanguage) to sort or filter the results, and then send the request. Datasources are not required to support the Chart Tools Datasource query language. If the Datasource does not support the query language, it will ignore the SQL query string, but still return a `DataTable`. The query language is a SQL language variant; read the full [query language syntax here](/chart/interactive/docs/querylanguage).\n4. Send the query, specifying a callback handler that will be called when the response is received: see next section for details.\n\n\nHere's an example of sending a request for data in a Google Spreadsheet cell range; to learn how\nto get the URL for a Google Spreadsheet, see\n[here](/chart/interactive/docs/spreadsheets#Google_Spreadsheets_as_a_Data_Source): \n\n```gdscript\nfunction initialize() {\n var opts = {sendMethod: 'auto'};\n // Replace the data source URL on next line with your data source URL.\n var query = new google.visualization.Query('http://spreadsheets.google.com?key=123AB&...', opts);\n\n // Optional request to return only column C and the sum of column B, grouped by C members.\n query.setQuery('select C, sum(B) group by C');\n\n // Send the query with a callback function.\n query.send(handleQueryResponse);\n}\n\nfunction handleQueryResponse(response) {\n // Called when the query response is returned.\n ...\n}\n```\n\nIf you are sending your query from within Apps Script, be sure to use [`IFRAME` mode](/apps-script/reference/html/sandbox-mode).\n\nProcessing the response\n-----------------------\n\n\nYour response handler function will be called when the request returns. The parameter passed in\nto your response handler function is of type\n[google.visualization.QueryResponse](/chart/interactive/docs/reference#QueryResponse).\nIf the request was successful, the response contains a data table\n(class `google.visualization.DataTable`). If the request failed, the response contains\ninformation about the error, and no `DataTable`.\n\n**Your response handler should do the following:**\n\n1. Check whether the request succeeded or failed by calling `response.isError()`. You shouldn't need to display any error messages to the user; the Visualization library will display an error message for you in your container `\u003cdiv\u003e`. However, if you do want to handle errors manually, you can use the [`goog.visualization.errors`](/chart/interactive/docs/reference#errordisplay) class to display custom messages (see the [Query Wrapper Example](/chart/interactive/docs/examples#querywrapper) for an example of custom error handling).\n2. If the request succeeded, the response will include a `DataTable` that you can retrieve by calling `getDataTable()`. Pass it to your chart.\n\nThe following code demonstrates handling the previous request to draw a pie chart: \n\n```gdscript\nfunction handleQueryResponse(response) {\n\n if (response.isError()) {\n alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());\n return;\n }\n\n var data = response.getDataTable();\n var chart = new google.visualization.PieChart(document.getElementById('chart_div'));\n chart.draw(data, {width: 400, height: 240, is3D: true});\n}\n```\n\nReading CSV files\n-----------------\n\nIf you want to build a chart out of CSV (comma-separated values)\ndata, you have two choices. Either manually convert the CSV data into\nthe [Google\nCharts datatable format](/chart/interactive/docs/datatables_dataviews#creatingpopulating), or place the CSV file on the web server\nserving the chart, and query it using the technique on this page.\n\nMore information\n----------------\n\n- [Query Language Syntax](/chart/interactive/docs/querylanguage) - Describes the syntax of the language used to make data queries.\n- [Query Class](/chart/interactive/docs/reference#Query) - Reference page for the class that wraps a query.\n- [QueryResponse Class](/chart/interactive/docs/reference#QueryResponse) - Reference page for the class that wraps the response to a query."]]