I used Google Apps Script Cache's putAll() for 21600 seconds. It has been working for months today just stopped working - all get from cache return null. The script reads a .csv file with about 90K entries, remove unwanted chars then caches using putAll() method. I tested the same file with the same script in my Dev's project it works fine. So somehow it does not work in my Production project. The code exactly the same, below:
My production's project id: Vendor Retainer GAS - project-id-5629958634838222671
There is no errors reported.
function cacheVendorList() {
var dataFilesFolder = DriveApp.getFolderById(dataFileFolderId);
var fileIt = dataFilesFolder.getFilesByName("VendorFile.csv");
if (fileIt != null && fileIt.hasNext()) {
var vendorFile = fileIt.next();
var keyValueStr = "{" + vendorFile.getAs(MimeType.PLAIN_TEXT).getDataAsString().
replace(/SAP,NAME,CMPY,CURRENCY/,'99,Test').
replace(/[^a-zA-Z0-9 ,\n]/g,' ').
replace(/$/mg,'",').
replace(/^\d+,/mg,
function(x) {
return '"' + x.substring(0,x.length-1) + '": "';
}).
replace(/D ",\n",$/,'D "') + "}";
//dataFilesFolder.createFile("VendorFile.txt", keyValueStr, MimeType.PLAIN_TEXT);
//Logger.log(keyValueStr);
var keyValObj = eval("(" + keyValueStr + ")");
// create a new vendor cache
vendorCache.putAll(keyValObj, 21600); // 21600 cache it for 6 hours
Logger.log("0000010100: " + vendorCache.get("0000010100"));
Logger.log("0090000435: " + vendorCache.get("0090000435"));
Logger.log("Got it");
}
}
Related
I'm in the process of redesigning my website, and with this I wanted to update/improve the various scripts involved. One such script is a call to the Twitch API for a set group of streamers, then displaying who's online and, if any are, displaying the first live stream and allow the user to switch between them.
My original script, which works great, was originally constructed by somebody here on StackOverflow. The problem is that instead of guiding me to a usable answer, they just rewrote my code to work in a way that I have a hard time following. I honestly have no idea how the loop they made works. It works, and I use it now, but that's my mistake. I want to start fresh and get a return for each user in an array, then construct things out based on the results of their live status. I don't understand how it is currently being done with this code, and I can't seem to figure out the point where it is saving the return, so I can't properly log it to the console to look at it and use it proper.
Long story short, can anybody help me with a simple return in a loop for each user so I can log it to the console and look at it and use it DIRECTLY instead of saving pieces to a million variables? Here's the code I'm currently using:
$(document).ready(function () {
var casters = ["marcusraven86","whit3rabbit87","dragonbornmonocle","m3ta1head","jimmyjojo7","redshoes","sergeantatams","stevechopz"];
var castersLive = [];
var reqs = $.map(casters, function (userName, i) {
return $.getJSON('https://api.twitch.tv/kraken/streams/' + userName + '.json?callback=?');
})
$.when.apply($, reqs).done(function () {
$.each(arguments, function (i, array) {
var data = array[0];
var userName = casters[i];
console.log(data);
if (data.stream === null) {
$('<div class="streampost"></div>').append(
'<a id="offline' + userName + '" class = "offline" href="http://www.twitch.tv/' + userName + '/profile" title="' + userName + '" target="_blank"></a>').appendTo('#statusblock');
}
else {
castersLive.push(casters[i]);
$('<div class="streampost"></div>').append(
'<a id="online' + userName + '" class = "online" href="http://player.twitch.tv/?channel=' + userName + '" title="' + userName + '" target="stream"></a>').appendTo('#statusblock');
}
});
console.log(castersLive);
if (castersLive.length > 0) {
$('<div class="streamplayer embed-responsive embed-responsive-16by9"></div>').append('<iframe src="http://player.twitch.tv/?channel=' + castersLive[0] +'" name="stream"></iframe><iframe src="http://www.twitch.tv/' + castersLive[0] + '/chat?popout=" frameborder="0" scrolling="no" height="478" width="350" id="chatFrame" name="chatFrame"></iframe>').appendTo('#streamblock');
}
else {}
$('.online').click(function() {
var streamName = $(this).attr('title');
$('#chatFrame').attr('src', 'http://www.twitch.tv/' + streamName + '/chat?popout=');
});
});
});
And here is where I'm trying to restart, but I can't seem to get the call to the API correct. I'm still a bit new to all this, and I also use the Tumblr and YouTube APIs. Each is different, and it throws me off a little.
$(document).ready(function () {
var casters = ["marcusraven86","whit3rabbit87","dragonbornmonocle","m3ta1head","jimmyjojo7","redshoes","sergeantatams","stevechopz"];
var castersLive = [];
for (i=0; i <= casters.length; i++) {
$.get('https://api.twitch.tv/kraken/streams/' + casters[i] + '.json?callback=?'), function ( data ) {
console.log(data);
};
};
A map I created last year using Google Maps API V3 and Fusion Tables V1 has stopped functioning properly in the last week or so. I am not sure if I missed an update that has deprecated my code or if there's another explanation. In brief, the following code queries my fusion table and if a match is found it returns data for an info window. However, it is now returning false every time. The addInfoWindow() function is firing fine. The issue appears to be either in the query itself or the data that's returned. Additionally, the pin is dropping in the correct location on the map so the coordinates are not the issue.
This issue can be replicated by entering an address in the field. For demonstration purposes, 9132 Kingston Pike 37923 should return true. Clicking inside any polygon will return the intended results.
Thank you for any guidance you can provide.
// query
var script = document.createElement('script');
var url = ['https://www.googleapis.com/fusiontables/v1/query?'];
url.push('sql=');
var query = "SELECT * FROM " +
tableid + " WHERE ST_INTERSECTS(geometry, CIRCLE(LATLNG(" + coordinate.lat() + "," + coordinate.lng() + "), 0.001))";
var encodedQuery = encodeURIComponent(query);
url.push(encodedQuery);
url.push('&callback=addInfoWindow');
url.push('&key=' + apiKey);
script.src = url.join('');
var head = document.getElementsByTagName('head')[0];
head.appendChild(script);
// call back function
function addInfoWindow(data) {
infowindow.close();
initialize();
var rows = data['rows'];
if (rows)
{
console.log("inside if statement");
for (var i = 0; i < 1; i++)
{
console.log("inside for loop: " + rows[i][0]);
infowindow.setContent("<div style='width:250px;'><h2>"+ rows[i][1] + "</h2><p>The neighborhood contact in your area would love to hear from you! </p><p>Click <a href='https://cspctystn.infellowship.com/GroupSearch/ShowGroup/" + rows[i][0] + "' target='_blank'>here</a> to get their information.</p><p> </p><p>If you desire to communicate with Community Life staff, contact -- removed --.<p><br/><br/></div>");
//console.log(rows[i][1] + ": " + rows[i][0]);
infowindow.setPosition(coordinate);
map.setCenter(coordinate);
map.setZoom(15);
infowindow.open(map);
}
}
else
{
console.log("error");
infowindow.setContent("<div style='width:250px;'><h2>Oops!</h1><p> It seems we don't have a neighborhood contact in your area.</p><p>Please communicate with our <a href='http://www.cspc.net/communitylife' target= '_blank' >Community Life</a> staff for more information. -- removed --<p></div>");
infowindow.setPosition(coordinate);
map.setCenter(coordinate);
map.setZoom(15);
infowindow.open(map);
}
}
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 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
When I run the application from Flash Develop everything is fine, both in Debug and Release.
After the application is packaged and installed, I just get a black screen when run.
However if I add a 'debug' file to applicationDirecty\META-INF\AIR\debug the application runs normally.
What could be causing this? and how can it be solved?
A debugging utility seemed to be causing the problem in the release build... removing calls to this solved the issue.
/** Gets the name of the function which is calling */
public function Log(prefix:String = "", suffix:String = "", params:* = null):void
{
var error:Error = new Error();
var stackTrace:String = error.getStackTrace(); // entire stack trace
var startIndex:int = stackTrace.indexOf("at ", stackTrace.indexOf("at ") + 1); //start of second line
var endIndex:int = stackTrace.indexOf("()", startIndex); // end of function name
var lastLine:String = stackTrace.substring(startIndex + 3, endIndex);
var functionSeperatorIndex:int = lastLine.indexOf('/');
var ClassSeperatorIndex:int = lastLine.indexOf(':');
var objectName:String = lastLine.substring(ClassSeperatorIndex+2, functionSeperatorIndex);
var functionName:String = lastLine.substring(functionSeperatorIndex + 1, lastLine.length);
trace(prefix +" " + "[" + objectName + "]" + " > " + functionName + " " + suffix);
}
Open Run.bat and replace
set OPTIONS=-connect %DEBUG_IP%
with
set DEBUG_IP=%DEBUG_IP%