我编写了一个在本机电子表格中使用时效果很好的脚本。我现在试图将它发布为附加组件,但我发现它onEdit(e)不起作用,何时onOpen(e)并且onInstall(e)工作正常。
我已经查看了有关授权模式和安装/启用附加组件的文档,但我认为我可能遗漏了一些东西(希望很简单),因为我是初学者。我应该以不同的方式调用函数吗?还是放置的位置onEdit?任何帮助表示赞赏。谢谢!!
function setup() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Send Auto Emails');
try {ss.setActiveSheet(ss.getSheetByName('Send Auto Emails'));}
catch (e) {ss.insertSheet('Send Auto Emails', 0);}
sheet.getRange(1, 1).setValue('Recipient Email Address');
//etc...
}
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = e.source.getActiveSheet();
var range = e.source.getActiveRange();
if (range.getA1Notation() == "C1" | range.getA1Notation() == "D1" | range.getA1Notation() == "E1" && sheet.getName() == "Send Auto Emails") {
Browser.msgBox(
'Alert',
'Feel …Run Code Online (Sandbox Code Playgroud) triggers google-sheets google-apps-script google-apps-script-addon
我正在尝试更改 Google 电子表格中第一行(标题)的背景颜色。基于几个小时的研究和阅读,我想出了以下代码:
Request updateBackgroundColorRequest = new Request();
UpdateCellsRequest updateCellsRequest = new UpdateCellsRequest();
CellFormat cellFormat = new CellFormat();
Color color = new Color();
color.setRed((float) 246);
color.setGreen((float) 178);
color.setBlue((float) 107);
cellFormat.setBackgroundColor(color);
GridRange gridRange = new GridRange();
gridRange.setStartRowIndex(0);
gridRange.setEndRowIndex(1);
gridRange.setStartColumnIndex(0);
gridRange.setEndColumnIndex(14);
updateCellsRequest.setRange(gridRange);
String field = "userEnteredFormat.backgroundColor";
updateCellsRequest.setFields(field);
updateBackgroundColorRequest.setUpdateCells(updateCellsRequest);
requestList.add(updateBackgroundColorRequest);
batchUpdateSpreadsheetRequest.setRequests(requestList);
Spreadsheets.BatchUpdate batchUpdate = service.spreadsheets()
.batchUpdate(spreadsheetId, batchUpdateSpreadsheetRequest);
batchUpdate.execute();
Run Code Online (Sandbox Code Playgroud)
然而,虽然我能够成功执行此代码(API 没有返回错误),但背景颜色根本没有改变(我期望它更改为橙色,但它仍然是白色)。
我错过了什么?
我尝试使用以下代码访问 Google Drive 中的 Google Sheets 文件:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open('my_test').sheet1
print(wks.get_all_records())
Run Code Online (Sandbox Code Playgroud)
然后,我在我的驱动器中创建了一个名为my_test的电子表格,并将其与credentials.json 中的电子邮件共享。然后它起作用了。但是,范围太广,我想使用
scope = ['https://www.googleapis.com/auth/drive.file']
Run Code Online (Sandbox Code Playgroud)
反而。但是如果我改变范围,它就找不到工作表:
File "/home/my_username/.local/lib/python3.6/site-packages/gspread/client.py", line 130, in open
raise SpreadsheetNotFound
gspread.exceptions.SpreadsheetNotFound
Run Code Online (Sandbox Code Playgroud)
这是为什么?我怎样才能改变它?我在谷歌上搜索了很多,对于大多数人来说,共享或重新共享电子表格是有效的。就我而言,问题仍然存在。
在使用 drive.file 范围解决方案为 Drive 元数据添加范围的解决方案执行查询时,我也尝试了此错误 500,但这会导致此权限错误,这可以看作是进度(因为它找到了文件):
gspread.exceptions.APIError: {
"error": {
"code": 403,
"message": "The caller does not have permission",
"status": "PERMISSION_DENIED"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,即使我将电子表格访问权限设置为“公开”,此错误仍然存在。
任何提示非常感谢!
哦,我还有一个更普遍的问题:服务帐户是否可以访问我的整个驱动器?还是仅适用于与服务帐户共享的所有内容?因为我只与它共享了一个电子表格,所以我对这里的权限究竟是如何工作感到有些困惑。
我已经为这个错误苦苦挣扎了好几个星期,并尝试了之前发布的与 Google Sheets 的 Python API 相关的问题的解决方案。
当我通过适用于 python 的 Google Sheets API 向电子表格发出“写入”请求时,我不断收到错误。该错误表明我正在提交无效的 JSON,但我已经针对交互式测试窗口(Google API Explorer)测试了 JSON 结构,并且来自那里的请求正确更新了我的工作表。
代码如下
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import datetime
import json
# Call the Sheets API
SPREADSHEET_ID = #mySheetID
RANGE_NAME = '2018_Raw Data!A3:A367'
months = { 0:"Jan", 1:"Feb",2:"Mar",4:"Apr",5:"May",6:"Jun",7:"Jul",8:"Aug",9:"Sep",10:"Oct",11:"Nov",12:"Dec"}
now = datetime.datetime.now()
date = str(now.day) +"-"+ months[now.month] + "-"+str(now.year)
day_of_year = now.timetuple().tm_yday
myRow = day_of_year+2
print (date)
print (myRow)
BWRange= …Run Code Online (Sandbox Code Playgroud) 我在 Apps Script 中为 Google Sheets 编写了一个自定义函数。目标是拥有一张自动计算谁欠谁多少钱的表(例如拆分账单)。
我的工作表是这样的:
第一张帐单(餐厅)要分给所有 5 人,第二张帐单要分给除彼得以外的所有 5 人,因为 B3 中没有 0。
我的 Apps 脚本函数的输入将是单元格 B1 到 F3(因此,值和名称)。该函数工作正常 - 它计算出正确的结果。我通过浏览器 (sheets.google.com) 和我的手机应用程序 (Google Sheets) 打开该电子表格。但是,在我的手机上,结果单元格(带有公式=calc_debt(B1:F3))经常只显示"Loading ..."。有什么问题?
为了完整起见,这里是自定义函数的代码:
function calc_debt(input) {
var credit = [0, 0, 0, 0, 0]; // credit[0] = Peter, credit[1] = Mark ...
for (var i = 1; i < input.length; i++) { // starting at i = 1 to skip the first row, which is the names!
// …Run Code Online (Sandbox Code Playgroud) 我正在使用官方 Google 教程连接到 Gmail 地址并使用Gmail API从电子邮件下载附件。
给出的示例代码是这样的:
try:
message = service.users().messages().get(userId=user_id, id=msg_id).execute()
for part in message['payload']['parts']:
if part['filename']:
file_data = base64.urlsafe_b64decode(part['body']['data']
.encode('UTF-8'))
path = ''.join([store_dir, part['filename']])
f = open(path, 'w')
f.write(file_data)
f.close()
except errors.HttpError, error:
print 'An error occurred: %s' % error
Run Code Online (Sandbox Code Playgroud)
我系统地得到一个KeyError:'数据'。
当我打印“零件”对象时,我得到了这个。我检查了所有电子邮件都包含附件,我看到“正文”键有“attachmentId”和“size”字段,但没有“数据”字段。
{'partId': '1',
'mimeType': 'application/x-zip-compressed',
'filename': 'Statement.zip',
'headers': [{'name': 'Content-Type', 'value': 'application/x-zip-compressed; name="Statement.zip"'},
{'name': 'Content-Description', 'value': 'Statement.zip'},
{'name': 'Content-Disposition', 'value': 'attachment; filename="Statement.zip"; size=317; creation-date="Fri, 05 Oct 2018 11:00:24 GMT"; modification-date="Fri, 05 Oct 2018 …Run Code Online (Sandbox Code Playgroud) 如何通过触发的函数名称而不是triggerarray id以编程方式删除Google Apps脚本基于时间的触发器?
我正在创建一个触发器,稍后我想按名称删除触发器.我试过这个:
ScriptApp.deleteTrigger("myfunction");
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我不想使用这种方法:
var triggers = ScriptApp.getProjectTriggers();
ScriptApp.deleteTrigger(triggers[0]);
Run Code Online (Sandbox Code Playgroud)
由于我同时有多个触发器,可能会删除错误的触发器!那么如何通过名称而不是触发器数组ID来删除触发器?
function demo(){
ScriptApp.newTrigger("myfunction")
.timeBased()
.everyMinutes(1)
.create();
}
function myfunction(){
// do some stuff here then stop the trigger by name
ScriptApp.deleteTrigger("myfunction");
}
}
Run Code Online (Sandbox Code Playgroud) 我一直在尝试确定向 Google Apps 脚本内容服务发出的 POST 请求的来源doPost();
function doPost(request) {
// Something like this:
var originURL = request.headers.URL; // http://myurl.com
}
Run Code Online (Sandbox Code Playgroud)
由于我已将其发布(“部署为 Web 应用程序”),“任何人,甚至匿名”,它具有以下标头:
access-control-allow-origin: *
Run Code Online (Sandbox Code Playgroud)
所以我想在我的代码中控制它的访问。
是否可以?
我想使用Google Cloud Storage API将所有文件夹放入给定的Google Cloud存储桶或文件夹中.
例如,如果gs://abc/xyz包含三个文件夹gs://abc/xyz/x1,gs://abc/xyz/x2和gs://abc/xyz/x3.API应该返回所有三个文件夹gs://abc/xyz.
它可以很容易地使用 gsutil
gsutil ls gs://abc/xyz
但我需要使用python和Google Cloud Storage API来实现.
原始代码基本上是基于 2 个不同的模板(2 个不同的 Google Doc 和 2 个不同的键)将 Google Doc 转换为 PDF;
从 Google 表单回复中,如果选择了标准部件,它将向用户发送一封附有报价单(PDF 格式)的电子邮件。如果没有,则会向用户发送不同的模板:
第一个模板 --> 报价单(标准件)
第二个模板 --> 已收到请求模板(非标准件)
所以我现在要做的不是使用 2 个不同的文档,而是希望将模板放在 1 个文档中,但只转换特定页面。例如,第 1 页中的模板 1 和第 2 - 4 页中的模板 2。
下面的代码只会将整个文档转换为 PDF,所以我想知道是否有特殊的方法或属性getpagenumber可以转换特定页面。
var docName = "insertdocnamehere";
var custName = "insertcustomernamehere";
var docTemplate = "insertgoogledockeyhere";
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+ custName)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = …Run Code Online (Sandbox Code Playgroud)