问题描述
TLDR; .py 脚本能否运行 Google Apps 脚本?
嗨!
通过使用 Google Api,我设法创建了一个可以访问和编辑 Google 表格的 .py 脚本。然后,我希望该程序能够在工作表中选择一个自定义 UI 选项卡并运行该函数(用内置的 Google Apps 脚本编写)。
如果有更简单的方法可以做到这一点,非常感谢任何输入! :)
我写的程序在这里:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
import datetime
scope = ["https://spreadsheets.google.com/Feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json",scope)
client = gspread.authorize(creds)
#^this authorizes the programs mail and stuff
sheet = client.open("Proggetest").sheet1 #opens sheet
Now = datetime.datetime.Now()
ctime = Now.strftime("%d.%m.%Y %H:%M:%s") #time and date
sheet.update_cell(5,5,"updated by prog. : " + ctime)
#updates cell 5E
#INSERT CODE THAT RUNS APPS SCRIPT HERE:)
工作表中的 UI 部分在这里(用内置的 Google Apps 脚本编写)
var ui = SpreadsheetApp.getUi();
function buildTESTmenu() {
ui.createMenu('Tests')
.addSubMenu(ui.createMenu('colours')
.addItem('Change a cells colour','TESTmenu.changeColour')) #.changeColour pulls a function that changes the cells colour
.addSubMenu(ui.createMenu('Data')
.addItem('Change number in cell','TESTmenu.setValue'))#.setValuepulls a function that changes the cells value to a set number
.addToUi()
}
非常感谢任何帮助!
谢谢!
解决方法
您可以从 google-apps-script 运行 google-apps-script-api 到 python。但是有limitations。最重要的是,服务帐户不能与 api 一起使用。接下来,当在执行 api 上下文中调用函数时,我认为您无法操作 ui
。