javascript - 仅使用 javascript 将信息提交到 Google 云端硬盘电子表格

标签 javascript spreadsheet google-drive-api google-drive-realtime-api

我基本上是在寻找一种仅使用 javascript 将信息提交到 Google Drive 电子表格的方法。更改特定单元格的值是我所追求的。有人知道这是否可能吗? 谢谢。

最佳答案

如果您有适当的客户端 ID 和浏览器的 API key ,这是它在本地主机和网站中工作的正确代码,我采用相同的 javascript 并将其放入 sencha-touch 和 phoneGap 应用程序,但它没有如果你对此有任何想法,就不要工作吗? :

<!DOCTYPE html>
<!--
  Copyright 2011 Google Inc. All Rights Reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<html>
  <head>
    <title>JavaScript API Example</title>

    <style type="text/css">
      body { font-family: Arial; }
    </style>
    <script type="text/javascript">
      // The clientId and apiKey are available at
      // https://code.google.com/apis/console. For more information, see
      // http://code.google.com/p/google-api-javascript-client/wiki/Authentication.
      var clientId = '958693490238-nti7k45ajor0287286o8a5p4m26um32n.apps.googleusercontent.com';
      var clientSecret = 'p--MYTDoaqliiiSmP4TPFZX5';
      var apiKey = 'AIzaSyCa_oJyC4vX1KAbNoi0n0Zyqf_ZWTp8J7U';
      var scopes = 'https://www.googleapis.com/auth/fusiontables';
      var tableId;

      // Initialize the client, set onclick listeners.
      function initialize() {
        gapi.client.setApiKey(apiKey);
        document.getElementById('create-table').onclick = createTable;
        document.getElementById('insert-data').onclick = insertData;
        document.getElementById('select-data').onclick = selectData;
        window.setTimeout(function() { auth(true); }, 1);
      }

      // Run OAuth 2.0 authorization.
      function auth(immediate) {
        gapi.auth.authorize({
          client_id: clientId,
          client_secret: clientSecret,
          scope: scopes,
          immediate: immediate
        }, handleAuthResult);
      }

      // Handle the results of the OAuth 2.0 flow.
      function handleAuthResult(authResult) {
        var authorizeButton = document.getElementById('authorize-button');
        var createTableButton = document.getElementById('create-table');
        if (authResult) {
          authorizeButton.disabled = true;
          createTableButton.disabled = false;
        } else {
          authorizeButton.disabled = false;
          authorizeButton.onclick = function() { auth(false); return false; };
        }
      }

      // Run a request to create a new Fusion Table.
      function createTable() {
        var tableResource = [];
        tableResource.push('{');
        tableResource.push('"name": "People",');
        tableResource.push('"columns": [');
        tableResource.push('{ "name": "Name", "type": "STRING" },');
        tableResource.push('{ "name": "Age", "type": "NUMBER" }');
        tableResource.push('],')
        tableResource.push('"isExportable": true');
        tableResource.push('}');
        runClientRequest({
          path: '/fusiontables/v1/tables',
          body: tableResource.join(''),
          method: 'POST'
        }, function(resp) {
          var output = JSON.stringify(resp);
          document.getElementById('create-table-output').innerHTML = output;
          tableId = resp['tableId'];
          document.getElementById('table-id-1').innerHTML = tableId;
          document.getElementById('table-id-2').innerHTML = tableId;
          document.getElementById('insert-data').disabled = false;
          document.getElementById('select-data').disabled = false;
          document.getElementById('create-table').disabled = true;
        });
      }

      // Run a request to INSERT data.
      function insertData() {
        var name = document.getElementById('name').value;
        var age = document.getElementById('age').value;
        var insert = [];
        insert.push('INSERT INTO ');
        insert.push(tableId);
        insert.push(' (Name, Age) VALUES (');
        insert.push("'" + name + "', ");
        insert.push(age);
        insert.push(')');
        query(insert.join(''));
      }

      // Run a request to SELECT data.
      function selectData() {
        query('SELECT * FROM ' + tableId);
      }

      // Send an SQL query to Fusion Tables.
      function query(query) {
        var lowerCaseQuery = query.toLowerCase();
        var path = '/fusiontables/v1/query';
        var callback = function(element) {
          return function(resp) {
            var output = JSON.stringify(resp);
            document.getElementById(element).innerHTML = output;
          };
        }
        if (lowerCaseQuery.indexOf('select') != 0 &&
            lowerCaseQuery.indexOf('show') != 0 &&
            lowerCaseQuery.indexOf('describe') != 0) {

          var body = 'sql=' + encodeURIComponent(query);
          runClientRequest({
            path: path,
            body: body,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Content-Length': body.length
            },
            method: 'POST'
          }, callback('insert-data-output'));

        } else {
          runClientRequest({
            path: path,
            params: { 'sql': query }
          }, callback('select-data-output'));
        }
      }

      // Execute the client request.
      function runClientRequest(request, callback) {
        var restRequest = gapi.client.request(request);
        restRequest.execute(callback);
      }
    </script>
    <script src="https://apis.google.com/js/client.js?onload=initialize"></script>
  </head>
  <body>
    <h1>
      Fusion Tables JavaScript Example
    </h1>
    <h2>
      (1) Authorize using OAuth 2.0
    </h2>
    <p>
      Click Authorize to start the OAuth 2.0 authorization flow. If you have
      already authorized, the button will be disabled.
    </p>
    <input type="button" id="authorize-button" value="Authorize"><br>

    <h2>
      (2) Create Table
    </h2>
    <p>
      Click "Create Table" to create an exportable Fusion Table called "People"
      with columns "Name" with type "STRING" and "Age" with type "NUMBER".
      <pre>
{
  "name": "People",
  "columns": [
    {
      "name": "Name",
      "type": "STRING"
    }, {
      "name": "Age",
      "type": "NUMBER"
    }
  ],
  "isExportable": true
}</pre>
    </p>
    <input type="button" id="create-table" value="Create Table"
        disabled="disabled">
    <p id="create-table-output"><i>table response goes here...</i></p><br>

    <h2>
      (3) Insert data
    </h2>
    <p>
      Insert data into the newly created table.
    </p>
    <pre>INSERT INTO <span id="table-id-1">[table_id]</span> (Name, Age) VALUES ([name], [age])</pre>
    <label>Name:</label>
    <input type="text" id="name"><br>
    <label>Age:</label>
    <input type="age" id="age"><br>
    <input type="button" id="insert-data" value="Insert data"
        disabled="disabled">
    <p id="insert-data-output"><i>insert response goes here...</i></p><br>

    <h2>
      (4) Select all the rows from the table
    </h2>
    <p>
      Select the data that has been inserted into the newly created table.
    </p>
    <pre>SELECT * FROM <span id="table-id-2">[table_id]</span></pre>
    <input type="button" id="select-data" value="Select data"
        disabled="disabled">
    <p id="select-data-output"><i>select response goes here...</i></p>
  </body>
</html>

关于javascript - 仅使用 javascript 将信息提交到 Google 云端硬盘电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14537569/

相关文章:

java - 适用于 Android 的 Google 云端硬盘 SDK : How to get folder id if I know folder title

javascript - 在 Javascript 集中查找最大值/最小值

ruby-on-rails - Ruby 电子表格不打开远程文件

javascript - 使用 React 和 SheetJS 导出到 excel

C# HttpWebRequest POST 数据 block

ruby - 是否有可能 'unload' ('un-require' ) 一个 Ruby 库?

javascript - 使用 jQuery 滚动功能

javascript - 如何在 Canvas 上制作 Raphael.js 元素 "wiggle"?

javascript - 图像上的简单 Javascript/CSS 文本叠加

java - Java/Groovy 中的电子表格解析器