问题
I have been using custom functions to break out simple mathematics into readable JavaScript, but am getting the following error:
Service invoked too many times in a short time: exec qps. Try
Utilities.sleep(1000)
between calls. (line 0).
I have tried sleeping for a random time, but that doesn't help. My functions look like this:
function conversationGrowthRate(clientCount, initialGrowthRate, conversationCount) {
//Utilities.sleep(Math.random() * 30000);
for (var i = clientCount; i > 10; i--) {
if (initialGrowthRate > 0) {
conversationCount += initialGrowthRate
initialGrowthRate -= 0.000003
}
}
return conversationCount;
}
function conversionGrowth(clientCount, conversationCount, initialConversionRate, conversionGrowthRate, maxConversionRate, coversPerBooking, initialConversationGrowthRate, initialConversationCount) {
//Utilities.sleep(Math.random() * 30000);
if (clientCount <= 50) {
return coversPerBooking * conversationCount * initialConversionRate;
}
else {
var firstFiftyClientConversations = conversationGrowthRate(50,initialConversationGrowthRate, initialConversationCount)*30*50; //~30
var additionalConversionGrowthRate = (clientCount-50) * conversionGrowthRate;
var totalConversionRate = initialConversionRate + additionalConversionGrowthRate;
var additionalClientConversations = conversationGrowthRate(clientCount-50, initialConversationGrowthRate, initialConversationCount) * 30 * (clientCount-50);
if (totalConversionRate < maxConversionRate) {
return coversPerBooking * ((firstFiftyClientConversations * initialConversionRate) + (additionalClientConversations * totalConversionRate));
}
else {
return coversPerBooking * (conversationCount * maxConversionRate);
}
}
}
function salesProductivity(currentExecs, prevMonthExecs, prevTwoMonthExecs, prevThreeMonthExecs, prevFourMonthExecs, salesPerExecPerMonth) {
//Utilities.sleep(Math.random() * 30000);
var firstMonthHires = currentExecs - prevMonthExecs;
var secondMonthHires = prevMonthExecs - prevTwoMonthExecs;
var thirdMonthHires = prevTwoMonthExecs - prevThreeMonthExecs;
var fourthMonthHires = prevThreeMonthExecs - prevFourMonthExecs;
var longerHires = prevFourMonthExecs;
return (secondMonthHires * (0.33 * salesPerExecPerMonth)) + (thirdMonthHires * (0.66 * salesPerExecPerMonth)) + (fourthMonthHires * (1 * salesPerExecPerMonth)) + (longerHires * salesPerExecPerMonth);
}
I changed nothing before it started working.
回答1:
Google Sheets runs custom functions remotely through a service called exec
. If there are "too many" calls (according to some undocumented quota, or, at least, I haven't found any such documentation) to any custom functions, this error will be emitted.
If the calls are happening because many cells are calling the custom functions, I don't see how Utilities.sleep(milliseconds)
can help since exec
would be called before sleep
is (indeed, it'll be exec
that'll be calling sleep
whether directly or indirectly).
As some comments have mentioned, converting the function to accept ranges and return arrays (and changing the sheet to accommodate said function) is the way to go (until Google add smarter throttling).
回答2:
This should work for any google service that's hitting a usage quota.
while(true) {
try {
return '<whatever's hitting the quota>';
}
catch(error) {
Utilities.sleep(1000);
}
}
- It doesn't give up
- It doesn't impose a delay unless needed
来源:https://stackoverflow.com/questions/52494465/service-invoked-too-many-times-in-a-short-time-exec-qps-google-sheets