下面的代码是根据我以前使用的代码修改的。这样的方法可以让您完全执行在execution time limit中无法完成的批处理。通过知道批处理中的一项操作需要多长时间,您可以在达到该限制之前在Properties中保存进度,并在programmatically create a trigger中保存进度以恢复处理。
您需要做一些事情才能使其正常工作:
- 您需要修改我的
getEmployees_()
函数以从您的配置文件中准确地获取员工。
- 正如我在评论中提到的那样,您需要重构代码,以便可以对单个员工执行复制。该代码应该放在我的
replicateForEmployee_()
函数中。
- 运行
determineTimes()
函数以查看获取员工和文件复制所需的时间。
- 在
determineTimes()
函数中插入replicate()
的结果。
determineTimes()
的输出将在“执行”页面(“视图”>“执行”)中可见。展开执行行,您将看到如图所示的持续时间。
/**
* Get all of the employees in the sheet. Assumes row in the sheet is one employee. Adjust the range as needed.
* @returns {Object[][]}
*/
function getEmployees_() {
var source = SpreadsheetApp.openById("configfile--xxxx");
var sheet = source.getSheetByName("Mastersheet");
return sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn()).getValues();
}
/**
* Refactor your current code so that this function will replicate all necessary files for a single
* employee. The function will be called for every single employee.
* @param {Object[]} employee - The row representing the employee in your config file
*/
function replicateForEmployee_(employee) {
// Execute your replication process for a SINGLE employee here
}
/**
* Run this,and then examine your script executions (View > Executions) to see how long each
* function takes. The resulting durations should replace the corresponding values in the replicate()
* function (I would recommend rounding up,so 458 --> 500ms).
*/
function determineTimes() {
console.time("timeToGetEmployees");
var employees = getEmployees_(); // Get all of the employees
console.timeEnd("timeToGetEmployees");
console.time("replicationDuration");
replicateForEmployee_(employees[0]); // Execute replication once
console.timeEnd("replicationDuration");
}
/**
* Replicates the files. When approaching the time limit,will save location and set a trigger to
* continue from stopping point. Emails user upon completion.
* @param {Boolean} isFromTrigger
* @returns {undefined}
*/
function replicate(isFromTrigger) {
var employees = getEmployees(); // Get all of the employees
var timeToGetEmployees = 1000; // Assume 1 second to execute getEmployees()
var replicationDuration = 500; // Number of milliseconds it takes to execute replication for one employee.
var maxExecutionTime = 360000 - timeToGetEmployees; // 6 mins - time already used to get employees
var continueFrom = 0; // If first execution,will be zero. Otherwise,get the index to start from.
if (isFromTrigger === true) {
continueFrom = parseInt(PropertiesService.getScriptProperties().getProperty("continueFrom"),10);
}
var lapsedTime = 0; // ESTIMATED amount of time that has lapsed since the replication started
for (var i = continueFrom; i < employees.length; i++) {
// Replicate files for the employee
replicateForEmployee_(employees[i]);
// Checks how much time you have left
lapsedTime += replicationDuration;
if (lapsedTime >= (maxExecutionTime - replicationDuration)) { // warn just before timing out
PropertiesService.getScriptProperties().setProperty("continueFrom",i + 1); // save the last iterator value + 1
createAfterTrigger("replicateFromTrigger",60000); // Start again in one minute
console.info("About to time out. Last iterator: " + i);
return;
}
}
deleteAllTriggers(); // Cleanup any lingering triggers
PropertiesService.getScriptProperties().deleteProperty("continueFrom"); // Clean up properties
// Send completion email
MailApp.sendEmail(Session.getEffectiveUser().getEmail(),"Replication Complete","");
}
/**
* Should ONLY ever be called from a clock trigger.
* @returns {undefined}
*/
function replicateFromTrigger() {
replicate(true);
}
/**
* Create a new trigger to execute the specified function after a certain amount of time.
* @param {String} functionName - The name of the function to execute
* @param {Number} milliseconds - The duration (in milliseconds) after the current time when the trigger should run
* @returns {undefined}
*/
function createAfterTrigger(functionName,milliseconds) {
ScriptApp.newTrigger(functionName)
.timeBased()
.after(milliseconds)
.create();
}
/**
* Deletes all triggers in the current project.
* @returns {undefined}
*/
function deleteAllTriggers() {
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
本文链接:https://www.f2er.com/3120331.html