Google Apps Script - exeption: Invalid argument: 0 - javascript

The function block below runs perfectly whenever I run it form Google Apps Script Editor, but generates an Exception: Invalid argument: 0 when I run it from the appropriate site (docs.google.com).
I do not know what this exeption means, and also how to solve.
Could someone help me please?
function assignEventsColor() {
for (var i = 0; i < myevents.length; i++) {
var thisevent = myevents[i];
var title = thisevent.getTitle();
if (/Comp/.test(title)) { thisevent.setColor(CalendarApp.EventColor.PALE_BLUE); }
else if (/Matr/.test(title)) { thisevent.setColor(CalendarApp.EventColor.GREEN); }
else {}
}
}

Related

Error accessing "autoAllocateChunkSize" in PDFJS

i have a new problem after firefox 99 update.
I have an extension that reads pdfs using PDFJS (version pdfjs-2.13.216). It always worked and after the last update it started to give: Error: Permission denied to access property "autoAllocateChunkSize"
The same extension, with tiny changes, works in chrome normally.
Below is the code:
function getAllTextPDF(urlPDF) {
return new Promise(function (resolve , reject ) {
var pdfjsLib = this['pdfjs-dist/build/pdf']; //this because of firefox
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
var loadingTask = pdfjsLib.getDocument(urlPDF);
//** from that line it no longer enters and generates the mentioned error **
loadingTask.promise.then(function(pdf) {
var numPages = pdf._pdfInfo.numPages;
var textoFinal = '';
var pagesPromises = [];
for (var i = 0; i < numPages; i++) {
(function (pageNumber) {
pagesPromises.push(getPageText(pageNumber, pdf));
})(i + 1);
}
Promise.all(pagesPromises).then(function (pagesText) {
for (var i = 0; i < pagesText.length; i++) {
textoFinal += pagesText[i];
}
if(textoFinal.length > 0){
resolve({text: textoFinal});
loadingTask.destroy();
delete pdfjsLib;
delete loadingTask;
}
});
}, function (reason) {
// PDF loading error
reject({reason});
return 0;
});
});
}
example URL of file to read (worked normally):
blob:https://dominio.com/4db038e1-ae8d-4a15-a78f-0d9d0a182c7c
The idea of ​​the method is simple, go through the pdf file and return all the text. However now as mentioned the error is generated. Detail that did not generate the error in version 32bit firefox, I believe it started in the last update of firefox 99.
I appreciate your help.

Check whether file exists in Google Drive by using trigger file IDs

I capture file ids from triggers and check whether file exists in drive by. The below script throws exception and terminates the scripts abruptly if any of the trigger associated file is missing or if it is trash as mentioned in the Apps script openById documentation (which is natural). How do I overcome this?
function getForms() {
try {
var formsList = [];
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
var fid = triggers[i].getTriggerSourceId();
if (fid) {
var title = FormApp.openById(fid).getTitle() == "" ? "Untitled" : FormApp.openById(fid).getTitle();
formsList.push([title, fid]);
}
}
return formsList;
} catch (e) {
;//catch errors
}
}
You may use the try catch statement wisely to avoid the Exception:
try {
if (fid) {
var title = FormApp.openById(fid).getTitle() == "" ? "Untitled" : FormApp.openById(fid).getTitle();
formsList.push([title, fid]);
}
} catch (e) {
Logger.log(e.message);
}
Use try catch inside for loop instead of outside in a useless place

Extending a google spreadsheet into a google web app

I was coding using the google scripts, when I came across a problem I've been struggling with for a couple days now. I am using Code.gs (a default page in creating a web app in google), when I called in data from a google spreadsheet to try and display it on a webpage. I had no problems with calling in the data add storing it into a array but now I am struggling with trying to return it to my javascript code. Can this be done or is there something else I can do to fix it? My code is below.
function getContents()
{
var sheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xum5t4a83CjoU4EfGd50f4ef885F00d0erAvUYX0JAU/edit#gid=0&vpid=A1');
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
var education = [];
for (var j = 0; j < values[i].length; j++) {
if (values[i][j]) {
if(j==1){
education[education.length] = values[i][j];
}
}
}
}
Logger.log(education);
return education;
}
From that Code.gs code i want it to return it to a Javascript function that says this:
function onNew(){
var input = google.script.run.getContents();
for(var = 0; i<input.length; i++)
{
$("#main").append("<div id='class'>"+input[i]+"</div>);
}
}
and whenever I try to run it says that it causes an error because it is undefined. Thanks in advance! Anything helps!
You need to use the withSuccessHandler(). Your variable input will not receive the return from google.script.run.getContents()
Separate out the client side code into two functions:
HTML Script
function onNew() {
google.script.run
.withSuccessHandler(appendEducationHTML)
.getContents();
};
function appendEducationHTML(returnedInfo) {
console.log('returnedInfo type is: ' + typeof returnedInfo);
console.log('returnedInfo: ' + returnedInfo);
//If return is a string. Convert it back into an array
//returnedInfo = returnedInfo.split(",");
for (var = 0;i < returnedInfo.length; i++) {
$("#main").append("<div id='class'>"+returnedInfo[i]+"</div>);
};
};

JavaScript Find Specific URLs.

Alright, I've been working on a userscript that redirects when a specific page is loaded.
This is what I have so far:
function blockThreadAccess() {
var TopicLink = "http://www.ex.com/Forum/Post.aspx?ID=";
var Topics = [ '57704768', '53496466', '65184688', '41182608', '54037954', '53952944', '8752587', '47171796', '59564382', '59564546', '2247451', '9772680', '5118578', '529641', '63028895', '22916333', '521121', '54646501', '36320226', '54337031' ];
for(var i = 0; i < Topics.length; i++) {
if(window.location.href == TopicLink + Topics[i]) {
// Execute Code
}
}
}
The function is called on the page load, but it doesn't seem execute the code.
What it's supposed to do is check to see if the user is on that specific page, and if he is then execute the code.
Say someone goes to this link - http://www.ex.com/Forum/Post.aspx?ID=54646501, it then redirects the use. I'm trying to make it efficient so I don't have to add a bunch of if statements.
try converting both to lower case before comparing
var loc = window.location.href.toLowerCase();
var topicLnk = TopicLink.toLowerCase();
for(var i = 0; i < Topics.length; i++) {
if(topicLnk + Topics[i] == loc) {
// Execute Code
}
}

Chrome and Firefox incompatibility

I have two frames, the expression running in first frame and calling highlightElements function in another frame. This expression works fine in Firefox:
parent.frames[0].highlightElements(lineNumbers, stringObj);
The highlightElements function (just for sure):
function highlightElements(lineNumbers, stringObj) {
// run through the cycle and highlight them
//for (var ln in lineNumbers) {
var length = lineNumbers.length;
for (var ln=0; ln<length; ln++) {
var elements = $('.no');
//for (var i in elements) {
var el_length = elements.length;
for (var i=0; i<el_length; i++) {
if (parseInt(elements[i].innerHTML) == lineNumbers[ln]) {
var badThing = "yes";
for (var nextElement = elements[i].next();
nextElement.className != '.no'; nextElement = elements[i].next()) {
if (nextElement.innerHTML == stringObj) {
badThing = "no";
nextElement.effect('highlight', {}, 'slow');
scrollIntoView(nextElement);
}
}
if (badThing == "yes") alert("Didn't find the object");
}
}
}
}
But in Chrome it produces the error "Uncaught TypeError: Property 'highlightElement' of object[objectDOMWindow] is not a function".
How to change the expression to make it runnable in Chrome? Thanks
Make sure both frames are under same domain and protocol. Chome blocks javascript access from frames to another if the domains/protocols don't match. If you are working locally, and not under a local domain (i.e. the url is something like file:///C:/etc/etc.html) then it won't work either.

Categories

Resources