如何使用JavaScript向Google电子表格发出POST请

如何使用JavaScript向Google电子表格发出POST请

本文介绍了如何使用JavaScript向Google电子表格发出POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从客户端向Google电子表格发布一些数据.在仔细阅读他们的v4文档之后,我还没有找到解决方法.

I am trying to post some data from the client side to the google spreadsheet. After looking hard through their documentation for v4 I haven't found a way.

推荐答案

如何使用JS中的电子表格API写入单元格

从本质上讲,您必须了解 XHR .我举一个例子.假设您要写入电子表格的A1:B1范围,例如说[A1]您好[B1]世界.

In essence, you have to know XHR. I'll give you an example.Let's say you want to write to A1:B1 range of your spreadsheet, say [A1]Hello [B1]World.

您的URI请求如下所示:

Your URI request would look like this:

PUT https://sheets.googleapis.com/v4/spreadsheets/{SPREADSHEET_ID}/values/Sheet1!A1:B1?valueInputOption=USER_ENTERED

请求正文:

{
           "range":"Sheet1!A1:B1",
           "majorDimension": "ROWS",
           "values": [
           ["Hello"," World"]
          ]
}

使用Google Sheetsv4在oauthplayground中做到了这一点,并且可以正常工作.

Did this in oauthplayground using Google Sheetsv4 and it worked.

如何在JS中应用它?

设置 JS快速入门后,找到一种在函数内调用此方法的方法:

After setting up the JS Quickstart find a way to call this inside a function:

var params = {
           "range":"Sheet1!A1:B1",
           "majorDimension": "ROWS",
           "values": [
           ["Hello","World"]
          ],
     }
  var xhr = new XMLHttpRequest();
  xhr.open('PUT', 'https://sheets.googleapis.com/v4/spreadsheets/{SPREADSHEET_ID}/values/Sheet1!A1:B1?valueInputOption=USER_ENTERED');
  xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
  xhr.send(JSON.stringify(params));

还要阅读阅读并阅读在Sheets v4中编写单元格.希望这个速成课程对您有所帮助.

Also read Reading & Writing cells in Sheets v4. Hope this crash course helps.

这篇关于如何使用JavaScript向Google电子表格发出POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:48