I need to fetch the contacts of iPhone in my app and I have written code for that. In some of the iPhones, few contacts are not genuine, I mean name is save as blank or by any random string like "6.w S" and in some number is not genuine. I want to fetch the contacts which are not like as shown in the image below as it is crashing my app. So what checks I need to put in order to avoid fetching contacts like that. I am using Swift and my code is also written below:
My Code:
var people = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(self.objAddressBook, nil, ABPersonGetSortOrdering()).takeUnretainedValue() as NSArray as [ABRecord]
for person in people
{
var multiPhones:ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
for phoneNumbers in 0..<ABMultiValueGetCount(multiPhones)
{
var phoneNumber = ABMultiValueCopyValueAtIndex(multiPhones, phoneNumbers).takeRetainedValue() as! String
var nameOfPerson: String = (ABRecordCopyCompositeName(person).takeRetainedValue() as! String)
var personRecordDic = ["name":nameOfPerson,"phone":phoneNumber]
self.contactsArray.addObject(personRecordDic)
}
}
Issue Causing Contactss
While fetching the contact add validation to your required fields.
Eg. If you are using Number,Email of contact then check wether those field have blank value or not. If blank value will be there then don't add to your list.
Related
I am new to Google Apps Script and coding in general and wasn't entirely sure how to do this. I want to create code that allows me to create a new set of Google Slides based on a Slides template using the relevant rows from a Google Sheets document.
function generateNewSlides() {
var wsID = "would insert worksheet URL ID here";
var ws = SpreadsheetApp.openById(wsID).getSheetByName("Data");
var data = ws.getRange(2, 1, ws.getLastRow()-1, 5).getValues();
>the above should get the relevant table from the sheet
data.forEach(function(info){
if(info[0]){
var firstname = info[0];
var surname = info[1];
var email = info[2];
var phone = info[3];
var image = info[4];
var presName = info[5];
>the above are columns where the different pieces of data would be taken from for the placeholders in the Slides template
var slidesTemplateID = "would insert slides template URL ID here";
var slidesTemplate = SlidesApp.openById(slidesTemplateID);
var template = slidesTemplate.getSlides();
var folderID = "would insert desired folder ID for saving in here";
>the above should get me the Slides template
template.makeCopy(presName,DriveApp.getFolderById(folderID)); **>line where error occurred**
var newPresentation = DriveApp.getFilesByName(presName).next().getUrl();
var Presentation = SlidesApp.openByUrl(newPresentation);
>the above should create a copy and then open it
var shapes = (Presentation.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{firstname}}',firstname);
shape.getText().replaceAllText('{{surname}}',surname);
shape.getText().replaceAllText('{{email}}',email);
shape.getText().replaceAllText('{{phone}}',phone);
shape.getText().replaceAllText('{{presname}}', presName)
});
>the above should replace all the placeholder tags in the template with the row data
}
});
}
Above is the code I have so far. The worksheet I am extracting data from has columns: first name, surname, email address, phone number, image (URL), and presentation name. When I try to run it I encounter an error on line 37 where it says template.makeCopy is not a function, however I am certain .makeCopy should be able to create a copy for it, no?
My main questions are:
1) What should I change to make it work, generating a new set slides for each row in the worksheet?
2) How can I add images to it replacing placeholder tags I've added in squares (not textboxes) in the template?
Thanks in advance!
Issue 1. makeCopy:
makeCopy(name, destination) is a method of the class File, which belongs to the Drive Service, not to the Slides Service. In your code, template is a list of Slides (you retrieve it by calling the method getSlides() from a Presentation). makeCopy cannot work here.
In order to make a copy of a Presentation, you should be using the Drive Service instead. You should replace these lines:
var slidesTemplate = SlidesApp.openById(slidesTemplateID);
var template = slidesTemplate.getSlides();
With this one:
var template = DriveApp.getFileById(slidesTemplateID);
Issue 2. Iterating through all shapes:
Next, you want to iterate through all shapes in your Presentation, and replace all placeholder tags with your desired text. In order to do that, you are using Presentation.getShapes(), which cannot work, since getShapes() is not a method of Presentation, but of Slide.
You should first iterate through all Slides in the Presentation, and for each Slide, iterate through all Shapes. You should replace these lines:
var shapes = (Presentation.getShapes());
shapes.forEach(function(shape){
// Replacing text lines
});
With these ones:
Presentation.getSlides().forEach(function(slide) {
slide.getShapes().forEach(function(shape) {
// Replacing text lines
})
});
Note:
In order to retrieve the copied presentation, you are currently doing this:
template.makeCopy(presName,DriveApp.getFolderById(folderID));
var newPresentation = DriveApp.getFilesByName(presName).next().getUrl();
var Presentation = SlidesApp.openByUrl(newPresentation);
There is no need to do this, you can just retrieve the ID of the created template, and open by ID, like this:
var copiedTemplate = template.makeCopy(presName,DriveApp.getFolderById(folderID));
var Presentation = SlidesApp.openById(copiedTemplate.getId());
Reference:
Slides Service
Drive Service
I'm trying to update 1 single item in the iphone contacts. I'm updating the email array and don't want to affect the others in the array.
For instance, I'm changing the "home" email address from my app, but if they also have a "work" email in their phone contacts it deletes it and only puts in the new one. I need to keep all others fields in the array. Same for phone or address.
This is my update:
let homeEmailEntry : String = email!
let homeEmail = CNLabeledValue(label:CNLabelHome, value:homeEmailEntry as NSString)
contactToUpdate.emailAddresses = [homeEmail]
let saveRequest = CNSaveRequest()
saveRequest.update(contactToUpdate)
try store.execute(saveRequest)
That adds the home email but wipes out the work email or anything else in the array.
You need to concatenate the existing email addresses with the new one(s) you're adding.
contact.emailAddresses = contactToUpdate.emailAddresses + [homeEmail]
Or alternatively, you could append it.
contact.emailAddresses.append(homeEmail)
I am unsure what happens here if there's already a home email address, but you can remove the existing home email address before appending a new one.
contact.emailAddresses = contact.emailAddresses.filter({ $0.label != CNLabelHome }) + [homeEmail]
I have a google spreadsheet which contains multiple sheets (or tabs) within it. Each sheet is populated from its own unique form. None of the forms are embedded in the spreadsheet.
Periodically, I need to delete all the data in the sheets, and also delete all the old responses which are saved in each of the forms. I can do this using a .gs script which resides in the spreadsheet. It accesses the form by its ID (the long string which appears in its URI). This requires the ID string to be hardcoded in my .gs script.
Ideally, I would like to access each form from the sheet object (i.e. the destination for each forms entries). Mock up code would look like this...
var ss = SpreadSheedApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var form = sheet.getMyAssociatedSourceForm(); // my dream method :-)
form.deleteAllResponses() // this method already exists
Does anyone know if this is possible? Or will I have to continue to use the ID (which is currently working)?
rgds...
I think you can do this without literally typing in ID's into your script. But, you would need to get every Form in your drive, loop through them all and get the destinationId() of every Form.
Google Documentation
Then compare the destinationId with the current spreadsheets ID, which you can get without needing to "hard code" it:
function deleteAllResponses() {
var thisSS_ID = SpreadsheetApp.getActiveSpreadsheet().getId();
var allForms = DriveApp.getFilesByType(MimeType.GOOGLE_FORMS);
var thisFormFile, thisFormFileID = "", thisForm, theDestID = "";
while (allForms.hasNext()) {
thisFormFile = allForms.next();
thisFormFileID = thisFormFile.getId();
thisForm = FormApp.openById(thisFormFileID);
try {
theDestID = thisForm.getDestinationId();
} catch(err) {
continue;
};
if (theDestID === "" || theDestID === undefined) {
continue;
};
if (theDestID === thisFormFileID) {
thisForm.deleteAllResponses();
};
};
};
I have not tested this, so don't know if it works. If it does, let me know in the comments section.
I am just getting familiar with the StackMob server side custom code sdk and i am trying to get a relationship field and iterate through it in the form of a String array. How do i do that ? is it possble to iterate through it without parsing it into an array?
DataService ds = serviceProvider.getDataService();
List<SMCondition> query = new ArrayList<SMCondition>();
query.add(new SMEquals("product_id", new SMString(jsonObj.getString("product_id"))));
List<SMObject> results = ds.readObjects("product", query);
SMObject product= results.get(0);
//product.getValue().get("categories"); how do i get this to be a String array?
At its simplest, that would look something like this:
List<SMValue> categories = (List<SMValue>)(rawObj.getValue().get("categories").getValue());
for (SMValue smString : categories) {
SMString stringValue = (SMString)smString.getValue();
//do whatever you want with the string value here
}
Obviously there are some unchecked casts in here, so you will want to add type/null checking to the appropriate sections depending on your data & schema.
I am using Titanium to develop a mobile app. I have stuck in a problem, where i want to show contact list. I used the following code to show contact list
Titanium.Contacts.showContacts({ });
I am getting the contact list, but it shows in sorting order of last name . I want the list to be shown in sorting order of first name.
Any help will be appreciated
there is a property Ti.Contacts.CONTACTS_SORT_FIRST_NAME hope this helps. There is also CONTACTS_KIND_ORGANIZATION, and CONTACTS_KIND_PERSON.
var g = Ti.Contacts.getAllGroups( );//Getting all the groups on the contacts table
var m = g[0].members();//select a group and check if it has members
Ti.API.info(m)// my group was empty so i have to add people
var p = Ti.Contacts.getAllPeople( )// get all the contacts
for (var i in p){//group and add people to your group
g[0].add(p[i]);
Ti.API.info(p[i].firstName);
Titanium.Contacts.save()// you have to save new changes in IOS
}
g[0].sortedMembers(Ti.Contacts.CONTACTS_SORT_FIRST_NAME);// FINALLY WE CAN SORT
m = g[0].members();// get the members
for (var i in m){// verify they are in order
Ti.API.info(m[i].firstName);
}