react-native - 如何通过 react native 应用程序创建新的谷歌电子表格?

标签 react-native google-sheets-api multi-device-hybrid-apps

我正在编写一个简单的应用程序,我想在用户想要的时候创建一个谷歌电子表格。 我只能到达下面的部分,在那里我可以从现有的谷歌电子表格中读取数据,但我无法对其进行转换,这样,在单击按钮时,我将能够创建一个新的谷歌电子表格。 有人可以帮助我了解我需要使用的语法吗? 我可以这样做吗?

import React, { Component } from 'react';

const API_KEY = '...';
const sheetID = '...';

class App extends Component {
  getSheetValues = async () => {
    const request = await fetch(
      `https://sheets.googleapis.com/v4/spreadsheets/${sheetID}/values/A1?key=${API_KEY}`
    );
    const data = await request.json();
    console.log(data);
    return data;
  };

  render() {
    return (
      <div className="App">
        <button onClick={this.getSheetValues}>Get sheet values</button>
      </div>
    );
  }
}

export default App;

最佳答案

下面的代码有效 - 现在我想找到一种方法来自动刷新访问 token ,而无需转到 https://developers.google.com/oauthplayground

'''
import React, { Component } from 'react';

class App extends Component {
  getSheetValues = async () => {
    const request = await fetch(
      `https://sheets.googleapis.com/v4/spreadsheets/${sheetID}/values/A1?key=${API_KEY}`
    );
    const data = await request.json();
    console.log(data);
    return data;
  };

  getNewSheet = async () => {
    const request = await fetch(
      `https://sheets.googleapis.com/v4/spreadsheets`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${ACCESS_TOKEN}`,
        },
        body: JSON.stringify({
          properties: {
            title: 'newSpreadSheetTest',
          },
        }),
      }
    );
    const data = await request.json();
    console.log(data);
    return data;
  };

  render() {
    return (
      <div className="App">
        <button onClick={this.getSheetValues}>Get Spreadsheet values</button>
        <br />
        <hr />
        <button onClick={this.getNewSheet}>Create New Spreadsheet</button>
      </div>
    );
  }
}

export default App;
'''

关于react-native - 如何通过 react native 应用程序创建新的谷歌电子表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61526278/

相关文章:

javascript - Visual Studio 多设备混合无法获取包信息错误

android - Cordova 应用程序不会恢复,但会在按主页按钮然后再次打开应用程序后重新启动

android - 如何在 Android 模拟器中 "shake"一个 Android 设备以调出开发菜单来调试我的 React Native 应用程序

javascript - FlatList scrollToIndex 超出范围

python - 如何使用 python 从公共(public)谷歌表中获取数据?

java - 使用 Google Sheets/Google Drive API 请求特定文件权限

android - 如何在 phonegap/Cordova 中合并或覆盖 Android 的 build.xml

reactjs - 如何将 React Native 集成到现有的 iOS 项目中

react-native - 如何在 React Native 中给 WebView 中的圆角半径

php - Google Sheets PHP API - 添加多个选项卡时出现问题