programmatically find the range of selected sheet

时间:2018-10-02 09:10:18

标签: javascript reactjs google-sheets google-sheets-api

I am trying to read the contents from the google sheet. I could read the content but the problem is I have to provide the name of sheet hard coded, in my case, there are two sheets one is Demo sheet and Demo sheet 2. In the below example, you can see the value for range i have provided hardcoded. Is there any way of finding the active sheet? There can be 10, 100 sheets. So, what is the way to get the selected(active) sheets so i can show the data of active sheets only?

Here is the code

export function load(callback) {
  window.gapi.client.load("sheets", "v4", () => {
    window.gapi.client.sheets.spreadsheets.values
      .get({
        spreadsheetId: "1i08RnKMYnY4NutwoBnGm1bXlVP_WUif1e8e8fj0QHjw",
        range: "Demo Sheet" // get active sheet here
      })
      .then(response => {
        const data = response.result.values;
        console.log("data", data);
        callback(data);
      });
  });
}

class GoogleSheet extends React.Component {
  componentDidMount() {
    window.gapi.load("client", this.initClient);
  }

  initClient = () => {
    // 1rFX4OKQ5zO5dFDl9fwBy3dJ3qlX4jlqNxsRS78wr0yE
    window.gapi.client
      .init({
        apiKey: config.apiKey,
        discoveryDocs: config.discoveryDocs
      })
      .then(() => {
        console.log("init client");
        load(this.onLoad);
      });
  };

  onLoad = data => {
    console.log("onLoad data", data);
  };

  render() {
    return <React.Fragment>Google Sheets</React.Fragment>;
  }
}

export default GoogleSheet;

0 个答案:

没有答案