我相信你的目标如下。
- 您想使用 python 将这些值放入公开共享的 Google 电子表格中。
- 在这种情况下,您想要未经授权访问电子表格。
为此,这个答案怎么样?
问题和解决方法:
为了将值放入公开共享的 Google 电子表格中,使用了 POST 方法。在这种情况下,需要使用访问令牌。另一方面,在GET方法的情况下,当使用Sheets API时,可以使用API密钥。端点就像exportLinks
,您无需 API 密钥即可检索值。这些是Google方面的规范。
在这种情况下,为了实现您的目标,我想提出以下两种模式。
模式一:
在这种模式中,我想建议使用从服务帐户检索的访问令牌来访问电子表格。在这种情况下,脚本可以更简单。
示例脚本:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
spreadsheetId = "###" # Please set the Spreadsheet ID.
scope = ['https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
worksheet = spreadsheet.sheet1
worksheet.update_acell('A1', 'sample')
- 在此示例脚本中,
sample
使用 gspread 将其放入公共共享电子表格中第一个选项卡的单元格“A1”。
模式2:
在这种模式中,我想建议使用 Google Apps 脚本创建的 Web Apps 作为包装 API 来访问电子表格。在这种情况下,python脚本更加简单。
Usage:
请执行以下流程。
1. 创建 Google Apps 脚本的新项目。
Web Apps 的示例脚本是 Google Apps 脚本。因此,请创建一个 Google Apps 脚本项目。
如果您想直接创建,请访问https://script.new/ https://script.new/。在这种情况下,如果您尚未登录 Google,则会打开登录屏幕。所以请登录谷歌。这样,Google Apps 脚本的脚本编辑器就打开了。
2. 准备脚本。
请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中。和请在高级 Google 服务中启用 Google Sheets API https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services。该脚本适用于 Web 应用程序。
function doPost(e) {
try {
const spreadsheetId = e.parameter.spreadsheetId;
const obj = JSON.parse(e.postData.contents);
const resource = obj.body;
const range = obj.arguments.range;
const valueInputOption = obj.arguments.valueInputOption;
Sheets.Spreadsheets.Values.update(resource, spreadsheetId, range, {valueInputOption: valueInputOption});
return ContentService.createTextOutput("ok");
} catch(e) {
return ContentService.createTextOutput(JSON.stringify(e));
}
}
- 在这种情况下,使用POST方法。
- 在此示例脚本中,作为测试脚本,使用 Sheets API 中的 Spreadsheets.values.update 方法将值放入电子表格中。
3. 部署 Web 应用程序。
- 在脚本编辑器上,通过“发布”->“部署为 Web 应用程序”打开一个对话框。
- Select "Me" for "Execute the app as:".
- Select "Anyone, even anonymous" for "Who has access to the app:".
- 在这种情况下,不需要请求访问令牌。我认为我推荐此设置来实现您的目标。
- 当然,您也可以使用访问令牌。届时,请将其设置为"Anyone".
- 单击“部署”按钮作为新的“项目版本”。
- Automatically open a dialog box of "Authorization required".
- 单击“查看权限”。
- 选择自己的帐户。
- 单击“此应用程序未经验证”处的“高级”。
- 点击“转到###项目名称###(不安全)”
- 单击“允许”按钮。
- 单击“确定”。
- Copy the URL of Web Apps. It's like
https://script.google.com/macros/s/###/exec
.
- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到Web Apps中。请小心这一点。
4. 使用 Web 应用程序运行该函数。
这是用于请求 Web 应用程序的示例 python 脚本。请设置您的 Web Apps URL、电子表格 ID 和范围。
import json
import requests
spreadsheet_id = '###' # Please set the Spreadsheet ID.
body = {
"arguments": {"range": "Sheet1!A1", "valueInputOption": "USER_ENTERED"},
"body": {"values": [["sample"]]}
}
url = 'https://script.google.com/macros/s/###/exec?spreadsheetId=' + spreadsheet_id
res = requests.post(url, json.dumps(body), headers={'Content-Type': 'application/json'})
print(res.text)
- 在此示例脚本中,
sample
被放置到公开共享的电子表格中第一个选项卡的单元格“A1”中。
- 在这种情况下,Python脚本中不需要授权,因为部署Web Apps时已经完成了授权。
Note:
- 当您修改Web Apps的脚本时,请将Web Apps重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请小心这一点。
参考:
- Web Apps https://developers.google.com/apps-script/guides/web
- 通过 Google Apps 脚本利用 Web Apps https://github.com/tanaikech/taking-advantage-of-Web-Apps-with-google-apps-script
- 高级谷歌服务 https://developers.google.com/apps-script/guides/services/advanced
- 通过 Google Apps 脚本发布 Google 电子表格 https://stackoverflow.com/q/56469509
- 电子表格.值.更新 https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update