1

感谢您的阅读!我现在正在学习如何使用 GAS,我无法删除我在谷歌电子表格上选择的特定行。当我使用 react app 和 google script api 尝试使用“axios.delete 方法”删除后,我得到了主题错误。

我已经使用 axios 传递了 GET 方法和 POST 方法。实际上,我可以从我的谷歌电子表格中获取并发布我的数据。

但删除不能很好地访问。

我发现这个错误 405 is not allowed to access my google sheet,但是为什么即使 post 方法可以访问,我也会收到这个错误?

我的 App 脚本或我的 react.js 代码还需要其他代码吗?我无法解决这个问题...我想解决这个错误并删除我选择的特定行。另外,我想知道这个错误的解决方法。你有什么主意吗 ?如果你有什么好主意,可以告诉我吗?

感谢您的阅读。

这是我的 App 脚本代码。

function doDelete(req, sheet) {
   var id = req.parameter.id;

   var Row = sheet.getLastRow();
   for (var i = 1; i <= Row; i++) {
      var idTemp = sheet.getRange(i, 1).getValue();
      if (idTemp == id) {
         sheet.deleteRow(i);
      }

   }

} 

这是我的 reactjs 代码。

import React,{ useState , Component } from 'react';
import Paper from '@material-ui/core/Paper';
import Grid from '@material-ui/core/Grid';
import axios from 'axios';

axios.defaults.baseURL = 'http://localhost:3000';
var optionAxios = {
  headers: {
      'Content-Type': 'application/json;charset=utf-8',
      'Access-Control-Allow-Origin':'*' ,
  }
}

const api = 'https://mygoogleappscriptapi.com/exec';

class  Price extends Component  {
  constructor(){
    super();
    this.state = {
      info: []
    };
    this.getInfo();
    this.createInfo = this.createInfo.bind(this);
    this.deleteInfo = this.deleteInfo.bind(this);
  };

 // accessed get!
  getInfo = () =>{
    axios.get(api)
    .then((res) =>{
      console.log(res.data)
      this.setState({
        info: res.data
      })
    })
  }


  // accessed post!
  createInfo = () =>{
    axios.post(api,{
      product: "hoge",
      price: 1000,
      miniLot: 1000,
      cartonSize: "40*30*50"
    },optionAxios)
    .then((res) => {
      this.getInfo(res);
    })
  }

  // cant't access delete!
  deleteInfo = (e) => {
    console.log(e);
   axios.delete(api,{
     id: e,
   },optionAxios)
   .then((res) =>{
     this.getInfo(res);
     console.log('success!');
   })
    
  }

  

render(){
      return (
        <div className={this.root}>
          <Grid container>
            <Grid item xs={11}>
              <button onClick={this.createInfo}>createButon</button>
                <Paper>
                   {this.state.info.map(info => <div key={info.id}>
                       {info.product}
                       <button onClick={() => this.deleteInfo(info.id)}>×</button>
                     </div>)}
                </Paper>
            </Grid>
          </Grid>
        </div>
      );
    }
}

export default Price;

4

2 回答 2

2

仅支持以下 HTTP 方法:

  • POST
  • GET

DELETE不支持该方法。

您可以使用帖子:

服务器端:

function doPost(e){
  if(e.parameter.option === "DELETE") return doDelete(e);
  /*rest of doPost here*/
}

反应:

  // convert to axios.post
  deleteInfo = (e) => {
    console.log(e);
   axios.post(api,{//modified
     id: e,
     option: "DELETE",//added
   },optionAxios)
   .then((res) =>{
     this.getInfo(res);
     console.log('success!');
   })
    
  }
于 2020-06-28T15:18:17.013 回答
0

试试这个:

function doDelete(req, sh) {
  var id = req.parameter.id;
  const ss=SpreadsheetApp.getActive();
  sh=sh||ss.getActiveSheet();
  var vs=sh.getRange(1,1,sh.getLastRow(),1).getValues();
  var d=0;
  for (var i=0;i<vs.length;i++) {
    if (vs[i][0]== id) {
      sh.deleteRow(i+1-d++);
    }
  }
} 
于 2020-06-28T14:01:46.737 回答