Context
A bit of context before we can dive into the code: I am currently working for a non-profit organisation for the protection of cats. I'm not a pro developer, I'm not paid to work on this, but since I'm the only one willing to do it and who knows a bit how to code, I volunteered to write a script for creating and updating adopter and abandoner contacts for our cats.
The other volunteers in the organisation are using Google Sheets to keep track of lots of information about the cats, including their adopters' and abandoners' contact information. On the other hand, the person in charge of the organisation wants to have every adopter or abandoner contact in a specific format in her Google Contacts. Before I wrote the script, volunteers used to enter all the info in the spreadsheet and enter it again in the boss' contacts.
The script is mostly functional and handles the update of contact information as well. However, the script creates duplicate contacts for some people, and I don't really understand why (although I may have a lead). It is a bug which only happens when volunteers use the script, but not when I use it; which makes me think something goes wrong when they call the script...
Code
The script creates an array of Person objects. Every person has a method to return a contactObject version of itself, compatible with Google's People API and I use People API's batchUpdateContacts function to create contacts, by batches of 200 while there are new contacts in the array.
In order to know the contacts already created, I first get the created connections using this function:
/** Helper function to list all connections of the current google user
*
* #returns {PeopleAPI.Person[]} connections - All of the connection objects from Google's People API
*/
/** Helper function to list all connections of the current google user
*
* #returns {PeopleAPI.Person[]} connections - All of the connection objects from Google's People API
*/
function getAllConnections_() {
var connections = [];
var apiResponse;
var nextPageToken;
var firstPass = true;
do {
if (firstPass) {
apiResponse = People.People.Connections.list('people/me', {'personFields': 'memberships,emailAddresses,phoneNumbers,names,addresses,biographies,userDefined'});
firstPass = false;
}
else {
apiResponse = People.People.Connections.list('people/me', {'personFields': 'memberships,emailAddresses,phoneNumbers,names,addresses,biographies,userDefined', 'pageToken': nextPageToken});
}
connections = connections.concat(apiResponse.connections);
nextPageToken = apiResponse.nextPageToken;
} while (nextPageToken);
return connections;
}
Then, I use a filter function to eliminate the already existing contacts based on the contacts email addresses (when a cat is adopted, we always ask for 2 email addresses, so I know there is at least one):
/** Helper function to filter the existing contacts and avoid creating them
*
* #param {Person[]} people - people to filter from
* #param {connections[]} connections - existing contacts in person's address book
* #returns {Person[]} filteredPeople - people who are not in connections
*/
function filterExistingContacts_(people, connections) {
if (!connections[0]) {
return people;
}
return people.filter(function (person) {
for (contact of connections) {
if (!contact.emailAddresses) {continue;}
if (contact.emailAddresses.filter(function (email) {return email.value.toLowerCase().replace(/\s/g, '').includes(person.email)}).length > 0) {return false;}
}
return true;
});
}
In the above code, person.email is lowercased and spaces are replaced by ''. When I run those functions, I can't reproduce the bug, but when the script users do, they get any number from 2 to 74 duplicate contacts created.
Supposition and leads
My supposition is that, maybe, the "getAllConnections_" function gets a bad response from Google's People API, for some reason and thus, gets an incomplete array of existing connections. Then my filter function filters correctly (since I can see no fault in my logic here) the contacts, but some existing contacts are re-created because the script is not aware they already exist.
First idea
If this is so, I think possibly a SQL database could solve the problem (and lower the complexity of the algorithm, which is quite slow with the current ~4000 existing contacts). But I don't really know where I could find a free database (for the organisation would much prefer paying for veterinary care than for this) which would function with Appscript ; plus that would mean a lot of work on the code itself to adapt it... So I would like to know if you guys think it may solve the problem or if I'm completely mistaken before I give it some more hours.
Second idea
Also, I thought about using something like the "ADDED" trick described here: Delete duplicated or multiplied contacts from Google Contacts as a workaround... But the spreadsheet is not structured per contact, but per cat. So it would lead to a problem for a specific situation which is, actually and sadly, quite frequent:
Patrick Shmotherby adopts the cat Smoochie → Smoochie's adopter column is marked as "ADDED" and Patrick's contact is created.
Patrick Shmotherby later abandons Smoochie → Smoochie's abandoner column is marked as "ADDED" and Patrick's contact is updated.
Karen Klupstutsy later adopts Smoochie → Smoochie's adopter column is already marked as "ADDED" so Karen's contact is not created.
A solution could be asking the volunteers to delete the "ADDED" marker manually, yet I think you can understand why this is error-prone when updating lots of contacts on the same day and time-consuming for the volunteers.
Third idea
I thought I might create a function for auto-deleting duplicate contacts from the Google account, but I would prefer not to use this solution as I'm afraid I could delete some important data there, especially as this is the boss' professional, organisational and personal account.
How you could help me
I must say, despite my leads, I'm a bit lost and confused by these duplicates, especially since I can't debug anything because I can't reproduce the bug myself. If you have any fourth lead, I would welcome it warmly.
Also, and because I'm a hobbyist, it's very likely that I didn't do things the correct way, or did not know I could do something else (e.g. I suggested using a SQL database because I know of the existence of relational databases, but maybe there are other common tools I've never heard of). So any suggestion would be good too.
Finally, if you think I'm correct on my own diagnosis, telling me so could help me get the motivation to re-write my code almost entirely if needed. And if you know where I could find a free database usable with Google Appscript (I know quality has a price, so I don't have much hope for this, but we never know) and if it's not "host your own database in you basement", that would be awesome!
Tell me if you need more information, if you want me to put some other piece of code or anything.
Have a nice day/afternoon/evening/night,
Benjamin
Alright so I found where the problem was from, thanks to #OctaviaSima who pointed me to the executions logs.
Apparently, for some reason I don't know, sometimes, my function "getAllConnections_()" which was supposed to get all the contacts in the Google Contacts book failed to get some contacts using this code:
/** Helper function to list all connections of the current google user
*
* #returns {PeopleAPI.Person[]} connections - All of the connection objects from Google's People API
*/
function getAllConnections_() {
var connections = [];
var apiResponse;
var nextPageToken;
var firstPass = true;
do {
if (firstPass) {
apiResponse = People.People.Connections.list('people/me', {'personFields': 'memberships,emailAddresses,phoneNumbers,names,addresses,biographies,userDefined'});
firstPass = false;
}
else {
apiResponse = People.People.Connections.list('people/me', {'personFields': 'memberships,emailAddresses,phoneNumbers,names,addresses,biographies,userDefined', 'pageToken': nextPageToken});
}
connections = connections.concat(apiResponse.connections);
nextPageToken = apiResponse.nextPageToken;
} while (nextPageToken);
return connections;
}
E.g. last execution, the actual contact list was 4061 connections long, however the script only got 4056 connections, which led to 5 duplicate contacts being created.
I added a quick patch by ensuring the connections table was as long as the number of contacts by calling the function recursively if it's not the case.
/** Helper function to list all connections of the current google user
*
* #returns {PeopleAPI.Person[]} connections - All of the connection objects from Google's People API
*/
function getAllConnections_() {
var connections = [];
var apiResponse;
var nextPageToken;
var firstPass = true;
do {
if (firstPass) {
apiResponse = People.People.Connections.list('people/me', {'personFields': 'memberships,emailAddresses,phoneNumbers,names,addresses,biographies,userDefined'});
firstPass = false;
}
else {
apiResponse = People.People.Connections.list('people/me', {'personFields': 'memberships,emailAddresses,phoneNumbers,names,addresses,biographies,userDefined', 'pageToken': nextPageToken});
}
connections = connections.concat(apiResponse.connections);
nextPageToken = apiResponse.nextPageToken;
} while (nextPageToken);
if (connections.length != apiResponse.totalItems) {connections = getAllConnections_();} // Hopefully, the magic lies in this line of code
return connections;
}
Posting this here in case it helps someone else.
Edit: Just corrected the test on the magic line from "==" to "!=".
Here is something I did for use with my email whitelist to ensure I didn't get duplicate emails.
function displayCurrentContacts() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Contacts');
sh.clearContents();
const vs = [['Name', 'Emails']];
const resp = People.People.Connections.list('people/me', { personFields: "emailAddresses,names,organizations" });
//Logger.log(resp);
const data = JSON.parse(resp);
let m = 0;
let n = 0;
data.connections.forEach((ob1, i) => {
if (ob1.emailAddresses && ob1.emailAddresses.length > 0) {
let emails = [... new Set(ob1.emailAddresses.map(ob2 => ob2.value))];//used set to insure I get unique list
//let emails = ob1.emailAddresses.map(ob2 => ob2.value);
let name;
m += emails.length;
//the following cases are derived from the way that I organize all of my contacts
if (ob1.names && ob1.organizations) {
name = ob1.names[0].displayName + '\n' + ob1.organizations[0].name;
++n;
} else if (ob1.names) {
name = ob1.names[0].displayName;
++n;
} else if (ob1.organizations) {
name = ob1.organizations[0].name;
++n;
}
vs.push([name, emails.sort().join('\n')])
}
});
vs.push([n, m])
sh.getRange(1, 1, vs.length, vs[0].length).setValues(vs)
sh.getRange(2, 1, sh.getLastRow() - 2, sh.getLastColumn()).sort({ column: 1, sortAscending: true });
}
Note this is not meant to be a plugin replacement by any means.
I'd like to completely undo any of Gmails built in category labels. This was my attempt.
function removeBuiltInLabels() {
var updatesLabel = GmailApp.getUserLabelByName("updates");
var socialLabel = GmailApp.getUserLabelByName("social");
var forumsLabel = GmailApp.getUserLabelByName("forums");
var promotionsLabel = GmailApp.getUserLabelByName("promotions");
var inboxThreads = GmailApp.search('in:inbox');
for (var i = 0; i < inboxThreads.length; i++) {
updatesLabel.removeFromThreads(inboxThreads[i]);
socialLabel.removeFromThreads(inboxThreads[i]);
forumsLabel.removeFromThreads(inboxThreads[i]);
promotionsLabel.removeFromThreads(inboxThreads[i]);
}
}
However, this throws....
TypeError: Cannot call method "removeFromThreads" of null.
It seems you can't access the built in labels in this way even though you can successfully search for label:updates in the Gmail search box and get the correct results.
The question...
How do you access the built in Gmail Category labels in Google Apps Script and remove them from an email/thread/threads?
Thanks.
'INBOX' and other system labels like 'CATEGORY_SOCIAL' can be removed using Advanced Gmail Service. In the Script Editor, go to Resources -> Advanced Google services and enable the Gmail service.
More details about naming conventions for system labels in Gmail can be found here Gmail API - Managing Labels
Retrieve the threads labeled with 'CATEGORY_SOCIAL' by calling the list() method of the threads collection:
var threads = Gmail.Users.Threads.list("me", {labels: ["CATEGORY_SOCIAL"]});
var threads = threads.threads;
var nextPageToken = threads.nextPageToken;
Note that you are going to need to store the 'nextPageToken' to iterate over the entire collection of threads. See this answer.
When you get all thread ids, you can call the 'modify()' method of the Threads collection on them:
threads.forEach(function(thread){
var resource = {
"addLabelIds": [],
"removeLabelIds":["CATEGORY_SOCIAL"]
};
Gmail.Users.Threads.modify(resource, "me", threadId);
});
If you have lots of threads in your inbox, you may still need to call the 'modify()' method several times and save state between calls.
Anton's answer is great. I marked it as accepted because it lead directly to the version I'm using.
This function lets you define any valid gmail search to isolate messages and enables batch removal labels.
function removeLabelsFromMessages(query, labelsToRemove) {
var foundThreads = Gmail.Users.Threads.list('me', {'q': query}).threads
if (foundThreads) {
foundThreads.forEach(function (thread) {
Gmail.Users.Threads.modify({removeLabelIds: labelsToRemove}, 'me', thread.id);
});
}
}
I call it via the one minute script trigger like this.
function ProcessInbox() {
removeLabelsFromMessages(
'label:updates OR label:social OR label:forums OR label:promotions',
['CATEGORY_UPDATES', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS', 'CATEGORY_PROMOTIONS']
)
<...other_stuff_to_process...>
}
I'm practicing with the early access Google App Maker and want to create a simple app that allows an administrator to change the password of another user in the organisation.
Whenever I try to call the Admin SDK API with something that would have previously worked with App Script, I get an error. It seems to be that App Maker is not allowing access to the SDK API.
I've enabled the Advanced Services > Google Admin Directory API. Is this where I should be able to enable the Admin SDK API (required for changing passwords)
To test, I'm trying to run this simple function:
function listUsers() {
var response = AdminDirectory.Users.list(optionalArgs);
var users = response.users;
if (users && users.length > 0) {
Logger.log('Users:');
for (i = 0; i < users.length; i++) {
var user = users[i];
Logger.log('%s (%s)', user.primaryEmail, user.name.fullName);
}
} else {
Logger.log('No users found.');
}
}
The above code returns this error:
AdminDirectory is not defined at NewPage.Button1.onClick:2:18
I'm sure I must be missing something here.
Many Thanks.
AdminDirectory (As well as other advanced services) are available on server side only.
You should move the method to Server Script and call it with google.script.run on button's click.
Please use code completion to see available options.
I'm trying to allow users of my Google Apps Script web app to switch google accounts. I've tried sending users to the Account Chooser via a hyperlink:
Switch Accounts
If the user chooses another account on that screen, the user returns to the web app still logged in under the original account.
What am I doing wrong, or is there another way of allowing users to switch accounts whilst on the web app?
While you might be needing Google Apps Script HTML Service to serve web pages that can interact with server-side Apps Script functions for your custom user interface, you might also need Admin SDK's Directory API to manage users.
As stated in the documentation,
The Admin SDK Directory service allows you to use the Admin SDK's Directory API in Apps Script. This API gives administrators of Google Apps domains (including resellers) the ability to manage devices, groups, users, and other entities in their domains.
First, you may use users: list to list all possible users. Example, you can list users in a domain which can be sorted by first name:
function listAllUsers() {
var pageToken, page;
do {
page = AdminDirectory.Users.list({
domain: 'example.com',
orderBy: 'givenName',
maxResults: 100,
pageToken: pageToken
});
var users = page.users;
if (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
Logger.log('%s (%s)', user.name.fullName, user.primaryEmail);
}
} else {
Logger.log('No users found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
}
Then, use users: get to retrieve a user.
Here's a sample code to get a user by email address and logs all of their data as a JSON string.
function getUser() {
var userEmail = 'liz#example.com';
var user = AdminDirectory.Users.get(userEmail);
Logger.log('User data:\n %s', JSON.stringify(user, null, 2));
}
Furthermore, please note that there are Directory API: Prerequisites and also needs authorization as described in Authorize requests in using this API.
This may seem to be a bit confusing so I suggest that you please go through the given links for more information.
Happy coding!
I have MCC account which i'm using to administrate couple AdWords accounts.
I wrote Script which checking base budget of each account and then weekly check how many did they spend. Then the script subtracts these values and then every data is saved into Google Drive (spreadsheet).
Main idea behind this script was to track current budget and then warn me when some account have no more money. But then I figured out that actually i do not need to track it because AdWords script already have functions which gives possibility to stop campaign.
So i digged little bit but everything i tried is not working.
Here is extracted function (example) with i using:
function main() {
var Customer = GetAccountData("XXX-XXX-XXXX");
StopCampaigns(Customer);
if(isCampaignRuning(Customer)){
Logger.log("Campaign is runing !");
}else{
Logger.log("Campaign is STOPPED!");
}
}
/*Helper functions */
function isCampaignRuning(account){
MccApp.select(account);
var campaigns = AdWordsApp.campaigns().get();
var IsCampaignRuning = false;
while(campaigns.hasNext()) {
var campaign = campaigns.next();
if(!campaign.isPaused()){
IsCampaignRuning=true;
break;
}
}
return IsCampaignRuning;
}
function StopCampaigns(account){
MccApp.select(account);
var campaigns = AdWordsApp.campaigns().get();
while(campaigns.hasNext()) {
var campaign = campaigns.next();
if(!campaign.isPaused()){
campaign.pause();
}
}
}
function GetAccountData(id){
var childAccounts = MccApp.accounts().withIds([id]).get();
if(!childAccounts.hasNext()){
return false;
}
var childAccount = childAccounts.next();
return childAccount;
}
Here is what's happening in console:
13:08:20.974 Campaign is runing !
In changes tab:
Change for: (Here is campaign name)
New value (if run now): temporarily suspended
type of change: Update > Status
Current value: Active
How can i correctly pause campaign ?
Is there better way to track current budget of accounts ?
For anyone who will look for answer to the same problem.
There was no problem in script itself.
"if run now" - should speak itself. I was using "Preview" to run script.
In that "preview" mode script doesn't change anything in account and campaign.
It just needed to be "run" normally.