I have been struggling with this error since about a week before DriveApp was released. I have a section of code that fails intermittantly with the error:
Service invoked too many times in a short time: driveWriteVolume rateMax. Try Utilities.sleep(1000) between calls.
Here is the code in question:
for(var a = 0; a<attachments.length; a++){
if(a > 0){
child = "." + (a + 1) + " ";
}
else{
child = ".1 ";
}
var parent = (m + 1);
Utilities.sleep(5000);
var file = attachmentFolder.createFile(attachments[a]);//This is the line that causes the error.
Utilities.sleep(1000);
file.rename(parent + child + attachments[a].getName());
}
I started with 1000ms, then gradually worked up to 5000 and it still throws the error every ~200 iterations. This is using DocsList.
I have had a similar problem in a script I'm working on.
The problem arises when I loop through the attachments to the message and attempt to save them too. If I skip the attachments, I don't have a problem, even with the same number of total file creations and the same sleep time between them. This is the code I'm using. newFolder.createFile(pdfBlob) does not cause a problem. newFolder.createFile(attachmentBlob) does.
// Create the message PDF inside the new folder
var htmlBodyFile = newFolder.createFile('body.html', messageBody, "text/html");
var pdfBlob = htmlBodyFile.getAs('application/pdf');
pdfBlob.setName(newFolderName + ".pdf");
newFolder.createFile(pdfBlob);
Utilities.sleep(sleepTime); // wait after creating something on the drive.
htmlBodyFile.setTrashed(true);
// Save attachments
Logger.log("Saving Attachments");
for(var i = 0; i < messageAttachments.length; i++) {
Logger.log("Saving Attachment " + i);
var attachmentBlob = messageAttachments[i].copyBlob();
newFolder.createFile(attachmentBlob);
Utilities.sleep(sleepTime); // wait after creating something on the drive.
} // each attachment
Related
I didn't do a great job of explaining the problem. The hanging behavior seems to be related to the use of console logging (with either Logger.log or console.log statements). If I comment out the logging statements, the hanging behavior does not occur. So, if I put in a breakpoint, the looping code will stop at the breakpoint one or a few times after clicking Resume and then just hang without getting back to the breakpoint. The number of successful times hitting the breakpoint before the hanging behavior occurs seems to be random. So, it seems that the problem is related to there being console logging in the loop. If I remove the logging statements, the hanging behavior does not occur. Thanks for any suggestions, here.
I have a loop that hangs in the debugger when a breakpoint{s) are set (but seemingly not when there are no breakpoints set). I've tried putting breakpoints in at various locations and then step over the breakpointed statement and then continue execution. Sometimes (randomly) it just never returns to the breakpoint and just runs forever (until timeout). Here's my code. Can anybody see what's going on. Thanks.
function getHikesDataByMonth() {
var firstHikeMonth = events[1][0].getMonth();
var thisHikeMonth = firstHikeMonth;
//var hikesColumn = firstHikeMonth;
var hikeDate, hikeDay,hikeName, hikeLevel, hikeNameShort;
var hikeMonth, newHikeMonth;
var eventRow = 1;
var hikeRow = 0;
var participantNumber = 0;
var numParticipants;
var hikeData = [];
var hikesColumn = [];
var hikeDate, hikeName, hikeNameShort, hikeLevel;
Logger.log("events.length " + events.length)
while (eventRow < events.length) {
//if (events[eventRow][4].isBlank) { break;}
if ((!events[eventRow][4].includes("hike") && !events[eventRow][4].includes("Hike") && !events[eventRow][4].includes("HIKE")) || events[eventRow][4].includes("CANCEL")) {
eventRow += 1;
continue;
}
// found a hike
console.log("Found Hike: " + events[eventRow][4]);
hikeName = events[eventRow][4];
hikeMonth = events[eventRow][0].getMonth() - firstHikeMonth;
if (hikeMonth > thisHikeMonth - firstHikeMonth) {
newHikeMonth = true;
thisHikeMonth = hikeMonth;
} else {
newHikeMonth = false;
}
hikeLevel = getHikeLevel(hikeName);
hikeNameShort = getHikeNameShort(hikeName);
hikeDate = events[eventRow][0];
hikeDay = weekDayNames[hikeDate.getDay()];
hikeData.push(hikeDay);
hikeData.push(hikeDate.toLocaleDateString('en-US'));
hikeData.push(hikeName)
hikeData.push(hikeNameShort);
hikeData.push(hikeLevel);
hikeData.push("");
hikeData.push("");
//hikeRowArray[0] = hikeRow;
//hikeData[hikeRow] = hikeRow;
console.log("Getting Participants: " + hikeName)
while (events[eventRow][4] == hikeName){
if (events[eventRow][9] != "Paid") {
eventRow += 1;
continue;
}
hikeData.push(events[eventRow][6] + " " + events[eventRow][5]);
eventRow += 1;
participantNumber += 1;
}
numParticipants = participantNumber
for (var i = participantNumber+1; i < 30; i++){
hikeData.push("");
}
//Logger.log(hikeParticipants[participantNumber]);
hikeData.push(numParticipants < 15 ? "Yes" : "No");
hikeData.push(15 - numParticipants);
// That's all for that hike - go to next hike
participantNumber = 0;
eventRow += 1;
hikeData.push(hikeData);
hikesColumn.push(hikeData);
console.log("Going to next hike at eventRow:" + eventRow)
if (newHikeMonth) {
hikesDataByMonth.push(hikesColumn);
}
}
}
This is a follow-up question derivated from How to solve error when adding big number of tables
With the code below, I get the following message when, for 500 tables. BUt it works fine for 200 for example.
Exception: Service Documents failed while accessing document with id
The error happens on line 22, inside de if body = DocumentApp.getActiveDocument().getBody();
You also have the table template id to try, but here is an image
Image Table Template
function RequirementTemplate_Copy() {
var templatedoc = DocumentApp.openById("1oJt02MfOIQPFptdWCwDpj5j-zFdO_Wrq-I48mUq9I-w");
return templatedoc.getBody().getChild(1).copy()
}
function insertSpecification_withSection(){
// Retuns a Table Template Copied from another Document
reqTableItem = RequirementTemplate_Copy();
var body = DocumentApp.getActiveDocument().getBody();
// Creates X number of separated tables from the template
for (var i = 1; i < 501; i++){
table = reqTableItem.copy().replaceText("#Title#",String(i))
body.appendTable(table);
if((i % 100) === 0) {
DocumentApp.getActiveDocument().saveAndClose();
body = DocumentApp.getActiveDocument().getBody()
}
}
}
It looks that the error message isn't related to the number of tables to be inserted because it occurs before adding the tables.
Just wait a bit an try again. If the problem persist try your code using a different account if the code runs on the second account it's very possible that you first account exceeded a limit... there are some limits to prevent abuse that aren't published and that might change without any announcement.
Using the fix suggested for the code from my answer to the previous question and changing the number for iteration limit to 1000 and 2000 works fine
The following screenshot shows the result for 1000
Here is the code used for the tests
function insertSpecification_withSection(){
startTime = new Date()
console.log("Starting Function... ");
// Retuns a Table Template Copied from another Document
reqTableItem = RequirementTemplate_Copy();
var body = DocumentApp.getActiveDocument().getBody();
// Creates X number of separated tables from the template
for (var i = 0; i < 2000; i++){
table = body.appendTable(reqTableItem.copy());
// if((i % 100) === 0) {
// DocumentApp.getActiveDocument().saveAndClose();
// }
//
}
endTime = new Date();
timeDiff = endTime - startTime;
console.log("Ending Function..."+ timeDiff + " ms");
}
function RequirementTemplate_Copy() {
//---------------------------------------------------------------------------------------------------------------------------------------------------
var ReqTableID = PropertiesService.getDocumentProperties().getProperty('ReqTableID');
try{
var templatedoc = DocumentApp.openById(ReqTableID);
} catch (error) {
DocumentApp.getUi().alert("Could not find the document. Confirm it was not deleted and that anyone have read access with the link.");
//Logger.log("Document not accessible", ReqTableID)
}
var reqTableItem = templatedoc.getChild(1).copy();
//---------------------------------------------------------------------------------------------------------------------------------------------------
return reqTableItem
}
function setReqTableID(){
PropertiesService.getDocumentProperties().setProperty('ReqTableID', '1NS9nOb3qEBrqkcAQ3H83OhTJ4fxeySOQx7yM4vKSFu0')
}
I'm trying to implement a chat-bot using GAS (google app script). And faced some troubles with LockService. It seems not to work properly or at all.
When i get new message from appropriate chat, i saved delay-value (8 seconds) for ignoring subsequent messages from this chat. I do this using lockService to avoid skipping concurrent messages.
However it doesn't work, and some messages leaved out.
My code:
function get_message_safely(chat_id, message) {
var lock = LockService.getScriptLock();
const delay = 8*1000; // 8 seconds
// save time of getting message
var get_time = (new Date()).getTime();
// lock
// set lock before accessing data
try {
if (lock.hasLock()) { // ==always false (for unknown reasons)
sendLog('get_message_safely/hasLock', 'lock = true' + '\n' + message);
}
lock.waitLock(10000); // wait 10 seconds for others' use of the code section and lock to stop and then proceed
} catch (error) {
sendLog('get_message_safely/lock_service', 'ОШИБКА: ' + error);
send_message(chat_id, "Наши сервера сейчас загружены. Отправьте сообщение повторно или попробуйте позже.");
lock.releaseLock();
return;
}
// get data from PropertiesService
var scriptProperties = PropertiesService.getScriptProperties();
var chat_access_flag = scriptProperties.getProperty(chat_id);
if (chat_access_flag != undefined) { // if chat already exist
if (chat_access_flag == 'false') { // access denied
sendLog('get_message_safely/lock_service', 'LockService: доступ запрещен.\n' + message);
lock.releaseLock();
return;
}
var lock_time = Number(chat_access_flag);
if (get_time <= lock_time) { // access denied: lock_time is still actual
sendLog('get_message_safely/lock_service', 'LockService: доступ запрещен т.к. время не прошло:\n' + lock_time + ' - ' + get_time + ' = ' + (lock_time - get_time) + '\n' + message);
lock.releaseLock();
return;
}
sendLog('get_message_safely/lock_service', 'LockService: доступ разрешен:\n' + lock_time + ' - ' + get_time + ' = ' + (lock_time - get_time) + '\nnew_lock_time: ' + (get_time + delay) + '\n' + message);
}
scriptProperties.setProperty(chat_id, get_time + delay); // if access is allowed, set new delay
// lock release
lock.releaseLock(); // release the lock
get_message(chat_id, message); // handling message
}
So, sometimes i have the case (when i send a lot messages at the same time), when lockService should handle it but it doesn't:
Case (example):
// lock_time == a;
// get 1 message: access allowed. So set new value to lock_time (=b)
// lock_time == b;
// get 2 message: access denied. get_time < lock_time (=b)
// lock_time still == b;
// get 3 message: access allowed: get_time > lock_time, where lock_time == a (!???). So set new value to lock_time (=c);
// lock_time == с (??)
// get 4 message: access denied: get_time < lock_time (=b)
Handling messages due to lockService should be done sequentially. But some messages for some reason access to PropertiesService and get same date at same time.
It's blowing my mind. Have you any ideas what's happening here?
And how can i reach desirable result?
After studying your code I see that .hasLock() always returns false. Please, forgive me if I am mistaken. In your scenario this could mean that neither .waitLock() nor .tryLock() was called before. To fix the issue you have to use one of the mentioned methods. You can read on the links some example usages. Please, ask me any question if you need further help.
I am trying to write a Google Apps Script that will process all emails that have a specific label.
I am using the GmailApp.search function to retrieve all of the relevant emails, but when I try to use the functions document in the GmailThread class, I get an error message that says that it can't find the function.
Here is my code;
var incoming = "To_Bot"
function readBotsEmail()
{
var emails = GmailApp.search("label:" + incoming);
Logger.log("This is the 'emails' object:" + emails)
var emailsLoopIndex = 0
for (var email in emails)
{
emailsLoopIndex += 1;
try
{
Logger.log("iteration " + emailsLoopIndex + " " + email.getMessageCount());
}
catch(e)
{
Logger.log("iteration " + emailsLoopIndex + " " + e);
}
}
}
Here is the logger output.
[14-01-26 03:40:00:909 EST] This is the 'emails' object:GmailThread,GmailThread
[14-01-26 03:40:00:911 EST] iteration 1 TypeError: Cannot find function getMessageCount in object 0.
[14-01-26 03:40:00:914 EST] iteration 2 TypeError: Cannot find function getMessageCount in object 1.
Where am I going wrong?
You should avoid using ambiguous variable names, "email" and "emails" are really bad choice when talking about threads on one side and index integer on the other...
Your issue comes mainly from this confusion between both variables, you used email instead of email*S* and also seem to forget that your value was an array of threads thus needing to be indexed.
Here is your working code, just one letter difference ;-) and a couple of brackets...
function readBotsEmail()
{
var emails = GmailApp.search("label:" + incoming);
Logger.log("This is the 'emails' object:" + emails)
var emailsLoopIndex = 0
for (var email in emails)
{
emailsLoopIndex += 1;
try
{
Logger.log("iteration " + emailsLoopIndex + " " + emails[email].getMessageCount());
}
catch(e)
{
Logger.log("iteration " + emailsLoopIndex + " " + e);
}
}
}
That said, you still have a lot of work on this script to make it return something interesting... for now it informs you on the number of threads and how many message they have... Anyway, that's a good start...
Good luck !
I recently started getting the error "Unexpected exception upon serializing continuation" on a spreadsheet Google Apps Script when trying to debug. The error seem to start after I created a connection to the Google CloudSQL api. This error still occurs even after commenting out the jdbc object constructor. It appears that others have had this issue and needed a Google Tech to resolve the issue.
I have searched all of the discussion boards for a solution to this issue with no luck. Any chance there is a Google tech out there who could take a look under the hood for me? I would post code if I could determine what line was actually triggering the error.
EDIT:
Ok, I think I have discovered where the error is occuring. Seems to be the
var response = UrlFetchApp.fetch(url + nextPage,oauth_options);
in the while loop. Here is the entire function code.
function retrieveEvents(endTimeMinimum, updatedAfter, orderBy){
//var url = 'https://www.googleapis.com/calendar/v3/calendars/' + source_cal + '/events?key=' + api_key + "&futureevents=true&orderBy=updated&sortOrder=descending&updatedMin=" + last_sync_date_formated;
//var url = 'https://www.googleapis.com/calendar/v3/calendars/' + source_cal + '/events?key=' + api_key + "&orderBy=updated&sortOrder=descending&updatedMin=" + last_sync_date_formated;
var url = 'https://www.googleapis.com/calendar/v3/calendars/' + source_cal + '/events?key=' + api_key + "&singleEvents=true";
if ((orderBy != null) && (orderBy != "")){
url += "&orderBy=" + orderBy;
}
else url += "&orderBy=updated";
if ((updatedAfter != null) && (updatedAfter != "")){
url += "&updatedMin=" + updatedAfter;
}
else url += "&updatedMin=" + last_sync_dateTime;
//if no endTimeMinimum is specified, the current time will be used.
if (endTimeMinimum == null || endTimeMinimum == ""){
endTimeMinimum = date_rfc339("Today");
}
url += "&timeMin=" + endTimeMinimum;
Logger.log("Request URL:" + url);
var largeString = "";
var events = new Array();
var nextPage = "";
var jsonObj
while(true){
var response = UrlFetchApp.fetch(url + nextPage,oauth_options);
largeString = response.getContentText();
if ((largeString != null) && (largeString != "")) {
jsonObj = JSON.parse(largeString);
}
if ('items' in jsonObj) events = events.concat(jsonObj.items);
if ('nextPageToken' in jsonObj){
nextPage = "&pageToken=" + jsonObj.nextPageToken;
continue;
}
break;
}
if (events.length == 0)return null;
return events;
}
OK, so I was able to make the problem go away by removing the try catch block inside a function that was called from inside a try catch block in the main function. I no longer am seeing the "Unexpected exception upon serializing continuation" when running the program from the debugger.
I wish I had a more solid answer on what causes this error and how to correct it.
In my experience, this is not an error caused by that line (or any other) specifically, but because an error is triggered within a loop. I haven't pinned down the exact replicable cause, but GAS seems to lose the loop pointer certain errors.
The best I can suggest is that any line that you suspect to causing an error within the while loop wrap with a try-catch that logs the error to the logger and proceeds. The loop pointer is then not lost and will debug as expected.