使用http清除谷歌电子表格数据

时间:2017-07-17 14:04:14

标签: google-sheets

我尝试使用http请求清除电子表格。

以下是我的http请求

  

https://sheets.googleapis.com/v4/spreadsheets/spresheetId/values/B2:B10?key=APIKEY

但是json返回错误

{
"error": {
"code": 403,
"message": "Requests from referer \u003cempty\u003e are blocked.",
"status": "PERMISSION_DENIED",
"details": [
  {
    "@type": "type.googleapis.com/google.rpc.Help",
    "links": [
      {
        "description": "Google developer console API key",
        "url": "https://console.developers.google.com/project/562822880211/apiui/credential"
      }
    ]
  }
]
}
}

所以如何使用http

清除我的电子表格

1 个答案:

答案 0 :(得分:1)

当您部署为webapp时,您将获得一个类似于以下内容的网址:https://script.google.com/macros/s/.../exec为其添加查询字符串,使其如下所示:https://script.google.com/macros/s/.../exec?ssid=spreadsheetid仅替换spreadsheetid以获取真实的电子表格ID

这是gs文件:

function clearSpreadSheet() 
{
   var ssid=PropertiesService.getScriptProperties().getProperty('SSID');
   var ss=SpreadsheetApp.openById(ssid);
   var allSheets=ss.getSheets();
   for(var i=0;i<allSheets.length;i++)
   {
     allSheets[i].clear();//clear all
     //allSheets[i].getRange(2,1,sheet.getLastRow(),sheet.getLastColumn()).clear();//everything but first row.   
   }
   return{'clearMessage':'Spreadsheet has been cleared.','ssid':ssid}
}

function doGet(e)
{
  PropertiesService.getScriptProperties().setProperty('SSID', e.parameter.ssid)
  var html = HtmlService.createHtmlOutputFromFile('clearss');
  return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)

}

这是clearss.html文件:

<!DOCTYPE html>
<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    $(function() {
    google.script.run
      .withSuccessHandler(dispCleared)
      .clearSpreadSheet();//runs when DOM is loaded
     });
     function dispCleared(data)//runs after spreadsheet is cleared via the SuccessHandler
     {
       $('#clrd').text(data.clearMessage);
       $('#ssid').text('SpreadSheet ID is ' + data.ssid);
     }
     console.log('My Code');
    </script>
  </head>
  <body>
    <h1 id="clrd"></h1>
    <h2 id="ssid"></h2>
  </body>
</html>

这是一个包含的网络应用。因此,请将这些文件加载​​到电子表格的脚本编辑器中。