跟踪帐户的Google Apps脚本的总执行时间 [英] Tracking total execution time Google apps script for an account
问题描述
Google Apps脚本每个帐户每天最多只能触发6个小时的触发脚本.
Google Apps script has the limitation of 6 hours triggered scripts per day per account.
我最近开始花太多时间在一天之内获取警报 Service
.
I recently started getting the alerts Service using too much computer time for one day
.
有没有一种方法可以跟踪按项目触发的脚本的总时间?
Is there a way track the total time of triggered scripts by project?
I found so far this post , but then I'd need to lookup all of my 50 projects.
谢谢
推荐答案
- 您要检索由时间驱动的触发器执行的功能的总执行时间.
- 通过Apps Script API中的"processes.list"方法检索功能的进程列表.
- 那时,使用
startTime
和endTime
设置检索总执行时间的时间段. - 使用进程类型的"TIME_DRIVEN"作为过滤器.这样,可以检索由时间驱动的触发器执行的功能的处理列表.
- 那时,使用
- Retrieve the process list of functions by the method of "processes.list" in Apps Script API.
- At that time, set the period for retrieving the total execution time using
startTime
andendTime
. - As the filter, "TIME_DRIVEN" of the process type is used. By this, the process list of functions executed by the time-driven trigger can be retrieved.
- At that time, set the period for retrieving the total execution time using
- 在脚本编辑器上
- 资源-> Cloud Platform项目
- View API控制台
- 在入门"中,单击探索并启用API".
- 在左侧,单击库.
- 在搜索API和服务,输入应用脚本".然后点击Apps Script API.
- 单击启用"按钮.
- 如果已启用此API,请不要关闭.
请在清单文件(
appsscript.json
)中添加以下范围.Please add the following scopes to the Manifest file (
appsscript.json
)."oauthScopes": [ "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/script.processes" ]
- 在脚本编辑器上
- 查看->显示清单文件
- 添加以上属性.
在运行此脚本之前,请设置
startTime
和endTime
.这是计算总时间的时间段.在此示例脚本中,计算了从2019-02-07T00:00:00.000Z
到2019-02-08T00:00:00.000Z
的总时间.Before you run this script, please set
startTime
andendTime
. This is the period for calculating the total time. In this sample script, the total time from2019-02-07T00:00:00.000Z
to2019-02-08T00:00:00.000Z
is calculated.function myFunction() { var startTime = "2019-02-07T00:00:00.000Z"; // Please set this. var endTime = "2019-02-08T00:00:00.000Z"; // Please set this. var params = {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}}; var data = []; var nextPageToken = ""; var url = "https://script.googleapis.com/v1/processes"; do { var query = "?fields=%2a" + "&pageToken=" + encodeURIComponent(nextPageToken) + "&userProcessFilter.types=TIME_DRIVEN" + "&userProcessFilter.startTime=" + encodeURIComponent(startTime) + "&userProcessFilter.endTime=" + encodeURIComponent(endTime); var res = JSON.parse(UrlFetchApp.fetch(url + query, params).getContentText()); Array.prototype.push.apply(data, res.processes); nextPageToken = res.nextPageToken || ""; } while (nextPageToken); var result = data.reduce(function(obj, e) { obj[e.functionName] = obj[e.functionName] ? obj[e.functionName] + Number(e.duration.replace("s", "")) : Number(e.duration.replace("s", "")); return obj; }, {}); Logger.log(result) }
结果:
作为示例情况,当时间驱动触发器运行
myFunction1
和myFunction2
的2个函数时,此脚本返回以下结果.结果意味着,对于startTime
和endTime
,myFunction1
和myFunction2
的总执行时间为123.456 s和12.345s.Result:
As a sample situation, when 2 functions of
myFunction1
andmyFunction2
are run by the time-driven trigger, this script returns the following result. The result means that forstartTime
andendTime
, the total execution times ofmyFunction1
andmyFunction2
are 123.456 s and 12.345 s, respectively.{ "myFunction1": 123.456, "myFunction2": 12.345 }
注意:
- 关于
startTime
和endTime
,这是RFC3339 UTC"Zulu"格式的时间戳,精确到纳秒.示例:2014-10-02T15:01:23.045123456Z
.这是来自官方文件. - 这是一个简单的示例脚本.因此,请根据您的情况进行修改.
- About
startTime
andendTime
, a timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds. Example:2014-10-02T15:01:23.045123456Z
. This is from the official document. - This is a simple sample script. So please modify it to your situation.
Note:
如果这对您的情况没有帮助,我深表歉意.
If this was not useful for your situation, I apologize.
这篇关于跟踪帐户的Google Apps脚本的总执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
如果我的理解是正确的,那么这种方法呢?通过更新Apps Script API,添加了用于检索项目过程的方法.我认为这可以用于上述情况.
If my understanding is correct, how about this method? By the update of Apps Script API, the method for retrieving the process of project was added. I thought that this can be used for above situation.
示例脚本:
准备工作:
在使用此脚本之前,请按以下步骤启用Apps脚本API.
Sample script:
Preparation:
Before you use this script, please enable Apps Script API as follows.