gmail app script cleaninbox modding - google-apps-script

Sorry, i'm not a programmer,
I'm using this gmail app script (CLEANINBOX) and it works great in roder to keep my inbox clean.
As I need to use INBOX app (mobile) and GMAIL app (desktop) at the same time I need to implement this script in order to avoid that PINNED messages in INBOX get archieved.
I inserted a condition in the IF sequence and it does not work
After struggling a bit I realized (correct me if I'm wrong) that the following code is not working becouse !thread.getLabels()=='PINNED') IS NOT BOOLEAN
Can anybody point me to the correct code?
function cleanInbox() {
var threads = GmailApp.getInboxThreads();
for (var i = 0; i < threads.length; i++) {
var thread=threads[i];
if (!thread.hasStarredMessages() && !thread.isUnread() && !thread.getLabels()=='PINNED') {
GmailApp.moveThreadToArchive(threads[i]);
}
}
}

Ok it was much easyer then expected...
I just needed to narrow down the number of threads to work with, and i did it just excluding those with "pinned" label
var threads = GmailApp.search('in:inbox -label:pinned')
solved
thanks for input

The statement !thread.getLabels()=='PINNED' is boolean (as the operand == outputs true or false). I think your problem is that thread.getLabels() does not return a string and therefore it will never be equal to 'PINNED'
EDIT:
The getLabels() method return value is GmailLabel[]. You can iterate over the labels and check your condition by adding this code:
for (var i = 0; i < threads.length; i++) {
var thread=threads[i];
if (!thread.hasStarredMessages() && !thread.isUnread()) {
var labels = thread.getLabels();
var isPinned = false;
for (var j = 0; i < labels.length; i++) {
if (labels[j].getName() == "PINNED"){
isPinned = true;
break;
}
if (!isPinned){
GmailApp.moveThreadToArchive(threads[i]);
}
}

Related

Validate a form with using Jquery - Problems

Sorry for my bad English.
Okay, let's go: when I try to validate my form using jQuery, it works just the else option.
I'm considering a spacing in between names, for identify the name input, for as fullname. But in doing so, nothing appears in my console.
Someone help me please.
$('form#form1').submit(function() {
var fname = $('input[name=fullname]').val()
var pho = $('input[name=phone]').val()
var mai = $('input[name=email]').val()
var amount = fname.split(' ').lenght
var splitStr = fname.split(' ')
if(amount >= 2) {
console.log('It worked!')
for(var i = 0; i < amount; i++ ) {
console.log(splitStr[1])
}
} else {
console.log("Wrong!")
return false;
}
return false;
}
It supposes to be fname.split(' ').length.
Writing fname.split(' ').lenght will result in amount == undefined and thus (undefined >= 2) will always be false.

Delete slides that contain a specific text string

I'm working on an automated slide setup and depending on some opt-out variables I need to remove some of the slides if they are not desired in the final output. To solve this I have created a script that adds a simple text string {{remove-this-slide}} to the slides that need to be deleted.
However, when trying to get a script to delete the slides containing that string it keeps deleting my entire presentation...
This is what I have:
function deleteFunction() {
var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides();
for (i = 0; i < currentPresentationSlide.length; i++) {
if (currentPresentationSlide[i].getPageElements().indexOf('{{remove-this-slide}}') > -1); {
currentPresentationSlide[i].remove();
}
}
}
Can anyone figure out what's going wrong here?
How about this modification?
Modification points :
The reason that the entire slides are deleted is ; after if (currentPresentationSlide[i].getPageElements().indexOf('{{remove-this-slide}}') > -1);. By this ;, if doesn't work and currentPresentationSlide[i].remove(); is always run.
The text data cannot be retrieved from currentPresentationSlide[i].getPageElements(). When you want to search the text from the text box, please use currentPresentationSlide[i].getShapes().
From your question, I was not sure where you want to search the text from. So I supposed that you want to search the text from shapes. The shape includes the text box.
Modified script :
function deleteFunction() {
var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides();
for (i = 0; i < currentPresentationSlide.length; i++) {
var shapes = currentPresentationSlide[i].getShapes();
for (j = 0; j < shapes.length; j++) {
if (shapes[j].getText().asString().indexOf('{{remove-this-slide}}') > -1) {
currentPresentationSlide[i].remove();
}
}
}
}
Reference :
getShapes()
If I misunderstand your question, I'm sorry.
There is a small bug in the code from #Tanaike. Because there might be more shapes in the same slide, you have to break the loop after deleting the slide.
Otherwise the code tries to traverse the shapes of a deleted slide, producing an error.
So the correct snippet looks like:
function deleteFunction() {
var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides();
for (i = 0; i < currentPresentationSlide.length; i++) {
var shapes = currentPresentationSlide[i].getShapes();
for (j = 0; j < shapes.length; j++) {
if (shapes[j].getText().asString().indexOf('{{remove-this-slide}}') > -1) {
currentPresentationSlide[i].remove();
break;
}
}
}
}

Programming Greatest Common Divisor for HTML Webpage

EDIT: So the question is, why will my script not execute properly.
Edit 2: So this is the part that works as advertised and all items are predefined as number inputs prior too.
<script>
function Script5(){
var numeratorIn = document.getElementById("Numerator").value;
var denominatorIn = document.getElementById("Denominator").value;
var FACTOR = document.getElementById("FACTOR").value;
var Snum = document.getElementById("Snum").value;
var Sden = document.getElementById("Sden").value;
var x = document.getElementById("FinalAnswer");
x.style.display = 'none';
var x = document.getElementById("FinalDisplay");
x.style.display = 'block';
}
</script>
So I am working on a series of codes for a website I will be developing and this is a small subroutine for finding greatest common divisors. I am using Notepad++ for mobility purposes and trying to run my code in Google Chrome to start. I want to make a GCD function for variables (numeratorIn,denominatorIn). Again in HTML
This is the part I want to add into the same script
var a = Math.floor(Math.sqrt(numeratorIn));
var b = Math.floor(Math.sqrt(denominatorIn));
document.getElementById("midpage15").innerHTML = (+a);
var k = 1
if (a<b) {
while (k<a) {
if ((Snum/k == Math.floor(Snum/k)) && (Sden/k == Math.floor(Sden/k)); {
var h = k;
}
k = k++;
}
}
else if (a>b) {
while (k<b) {
if ((Snum/k == Math.floor(Snum/k)) && (Sden/k == Math.floor(Sden/k)) {
var h = k;
}
k = k++;
}
}
else (a == b) {
document.getElementById("midpage15").innerHTML = ("Final Answer 1");
}
But it breaks my button that I use to activate the script every time :(
I usually program in Python but I want to make this in HTML. So I spent about 5 hours working on it and it is just driving me nuts. I am 100% confident it has something to do with the Ifs and the while statements at the bottom.
Here Snum and Sden are string.So you need to convert it into integer first.For that you can use parseInt() function.

find the length of a string in google script

I'm trying to make a script for google sheet, who can count a letter in a text. But it seems that .length doesn't work. Anyone who can give directions on where to find the the solution.
function Tjekkode(tekst , bogstav){
var test = "";
// find the length of laengdeTekst
var laengdeTekst = tekst.length;
var t = 0;
// through the text character by character
for ( var i = 1; i<laengdeTekst ; i++) {
var test = tekst.substr(i,1);
if (test == bogstav) {
// if the letter is found, it is counted up
// REMEMBER == means compare
var t = t + 1;
}
}
// returns percent appearance of the letter
Return = t / længdeTekst * 100
}
Thanks in advance
length is ok in your code. To test it, run this script:
function test( ) {
var test = "123456";
// finder længden på teksten
var laengdeTekst = test.length;
Logger.log(laengdeTekst);
}
After you run it, check Log, press [Ctrl + Enter]
The correct code in your case:
function Tjekkode(tekst, bogstav) {
var test = "";
var laengdeTekst = tekst.length;
var t = 0;
// start looping from zero!
for ( var i = 0; i<laengdeTekst; i++) {
var test = tekst.substr(i,1);
if (test == bogstav) {
var t = t + 1;
}
}
// JavaScript is case sensitive: 'return != Return'
return t / laengdeTekst * 100;
}
Please, look at this tutorial for more info
thanks
I'll guess that I might get the one with the R instead of r at the end, but the script didn't run that line, because it kinda stopped at the .length line :/
the comments in danish is for my pupils (I'm a teacher in elementary school)
I'll see if google wants to cooperate today :|
This is the google script that worked for me. Note the 24 - that's the length of an empty message that has markup like <div>...</div>
function TrashEmptyDrafts() {
var thread = GmailApp.getDraftMessages();
for (var i = 0; i < thread.length; i++) {
b=thread[i].getBody();
if (b.length <= 24.0){
thread[i].moveToTrash();
}
}}

How to adapt script for different 'from' addresses and folder names?

I have a Google Apps Script which searches the inbox for messages from a certain email address, adds a new label and then archives the email so as to remove the inbox tag. I'm wondering, is there an easy way to apply the script to many different email addresses and apply different filing labels without making an individual script or having a bloated single script with all the different email addresses in it?
function _getFilingLabel() {
var label_text = "**folder name for filing**";
var label = GmailApp.getUserLabelByName(label_text);
if (label == null) {
var label = GmailApp.createLabel(label_text);
}
return label;
}
function addfilelabel() {
var label = _getFilingLabel();
var threads = GmailApp.search('from:(**email address here**) label:inbox older_than:30d');
for (var i = 0; i < threads.length; i++) {
label.addToThread(threads[i]);
}
}
/**
* SCAN THROUGH THE "**folder name for filing**" label and unlabel any items that aren't currently in the inbox
*/
function removeinboxlabel() {
// Every thread in label is archived to remove the inbox label.
var threads = GmailApp.search('label:"**folder name for filing**"');
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
}
}
function incorrectfiled() {
// Checks for items incorrectly filed in folder with activity newer than 30d.
var threads = GmailApp.search('label:"**folder name for filing**" newer_than:30d');
for (var i = 0; i < threads.length; i++) {
threads[i].moveToInbox();
}
}
The first step you can take toward making your script re-usable is to replace your hard-coded values with parameters. For example, here's addfilelabel() with parameters for from_address and label_text, with the balance of the functions following in a snippet:
function addfilelabel(from_address, label_text) {
var label = _getFilingLabel(label_text);
var threads = GmailApp.search('from:(%FROM%) label:inbox older_than:30d'
.replace('%FROM%',from_address));
for (var i = 0; i < threads.length; i++) {
label.addToThread(threads[i]);
}
}
function _getFilingLabel(label_text) {
var label = GmailApp.getUserLabelByName(label_text);
if (label == null) {
var label = GmailApp.createLabel(label_text);
}
return label;
}
function addfilelabel(from_address, label_text) {
var label = _getFilingLabel(label_text);
var threads = GmailApp.search('from:(%FROM%) label:inbox older_than:30d'
.replace('%FROM%',from_address));
for (var i = 0; i < threads.length; i++) {
label.addToThread(threads[i]);
}
}
/**
* Scan the given label and archive any items that aren't currently in the inbox
*/
function removeinboxlabel(label_text) {
// Every thread in label is archived to remove the inbox label.
var threads = GmailApp.search('label:"%LABEL%"'
.replace('%LABEL%',label_text));
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
}
}
function incorrectfiled(label_text) {
// Checks for items incorrectly filed in folder with activity newer than 30d.
var threads = GmailApp.search('label:"%LABEL%" newer_than:30d'
.replace('%LABEL%',label_text));
for (var i = 0; i < threads.length; i++) {
threads[i].moveToInbox();
}
}
Note how from_address is placed into the search string using the JavaScript String.replace() method to fill in a placeholder. There's no magic with the placeholder string '%FROM%', this is simply a convention intended to make the placeholder stand out and to decrease the likelihood of accidentally replacing the wrong text.
Now you can call the function like this, which is one simple way to reuse the basic code you've written:
function do_addfilelabel() {
addfilelabel("user1#example.com", "from-user1");
addfilelabel("user2#example.com", "from-user2");
addfilelabel("user3#example.com", "from-user3");
}
From there, you could progress to have a user interface in a Web Application, say. The Web Application could have a simple user interface allowing you to enter an email address and label, then pass those to your server-side function(s) as parameters with google.script.run.