Write Google sheet cell - ios

I try to find how modify or write a cell in a google sheet.
I success to read my sheet (on my drive) with quickstart guide (I had copied and pasted this code : https://developers.google.com/sheets/quickstart/ios#step_3_set_up_the_sample). I had just changed the url by :
https://sheets.googleapis.com/v4/spreadsheets/my_spreadsheet_Id/values/Feuil1!A1:F
.
But impossible to find a code to write on cells of my sheet... when i look : https://developers.google.com/sheets/guides/values#methods. I don't understand where i should put my new data to the cell.
Exemple : i have "New York" on the cell A1.
i want to change "New York" by "Tahiti".
Do you know how do that ?
i tried this but not working :
- (void)modifyListe {
NSString *baseUrl = #"https://sheets.googleapis.com/v4/spreadsheets/";
NSString *spreadsheetId = #"{MySpredsheet_ID}"; // choisir la bonne
NSString *range = #"/values/Feuil1!G1:G1?valueInputOption=Tahiti";
baseUrl = [baseUrl stringByAppendingString:spreadsheetId];
baseUrl = [baseUrl stringByAppendingString:range];
[self.service fetchObjectWithURL:[NSURL URLWithString:baseUrl]
objectClass:[GTLObject class]
delegate:self
didFinishSelector:#selector(displayMajorsWithServiceTicketT:finishedWithObject:error:)];
}
SOLUTION : Look second post

I think found the solution (inspired by this post) :
NSString *baseUrl = #"https://sheets.googleapis.com/v4/spreadsheets/MyspreadsheetID/values/Donnees!G1:G1?valueInputOption=USER_ENTERED";
NSURL *theURL = [NSURL URLWithString:baseUrl];
NSString *rangeKEY = #"range";
NSString *dimensionKEY = #"majorDimension";
NSMutableString *valuesKEY = [NSMutableString stringWithString:#"values"];
NSString *therange = #"Donnees!G1:G1";
NSString *themajorDimension = #"ROWS";
NSMutableString *string_Value = [NSMutableString stringWithString:#"theValue"];
NSMutableArray *ArrayOfString = [NSMutableArray array];
NSMutableArray *arrayOfArray = [NSMutableArray array];
[ArrayOfString addObject:string_Value];
[arrayOfArray addObject:ArrayOfString];
NSMutableDictionary *dicooo = [NSMutableDictionary dictionary];
[dicooo setObject:arrayOfArray forKey:valuesKEY];
[dicooo setObject:therange forKey:rangeKEY];
[dicooo setObject:themajorDimension forKey:dimensionKEY];
GTLObject *theobject ;
theobject = [GTLObject objectWithJSON:dicooo];
[self.service fetchObjectByUpdatingObject:theobject forURL:theURL delegate:self didFinishSelector:#selector(displayMajorsWithServiceTicketT:finishedWithObject:error:)];
When I launch I can see the modification on my sheet.

Related

Set title property from NSarray in CSSearchableItemAttributeSet

I am trying to using CoreSpotlight API in application , I have plist file which has a several items on it for example animals' name . So I need to set title string equal to on of those object , for example if users search Lion , the line name and for example its features appears on the spotlight . Here is my code :
- (void)setupCoreSpotlightSearch
{
CSSearchableItemAttributeSet *attibuteSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(__bridge NSString *)kUTTypeImage];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"animals" withExtension:#"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];
NSString *getNames = [NSString stringWithFormat:#"%#",playDictionariesArray];
NSLog(#"%#",getNames) ;
attibuteSet.title =getNames;
attibuteSet.contentDescription = #"blah blah ";
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:#"app name"
domainIdentifier:#"com.compont.appname"
attributeSet:attibuteSet];
if (item) {
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:#[item] completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(#"Search item indexed");
}
}];
}
}
The problem is getNames returns all names !!! how can I filter it when is user is searching an specific word from animals.plist
Thanks .
EDIT [Plist Image]:
You can maintain NSArray and iterate through playDictionariesArray, creating & initialising CSSearchableItem object with that particular entry in your data source.
- (void)setupCoreSpotlightSearch
{
NSURL *url = [[NSBundle mainBundle] URLForResource:#"animals" withExtension:#"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];
NSMutableArray * searchableItems = [[NSMutableArray alloc]init];
for(object in playDictionariesArray)
{
CSSearchableItemAttributeSet *attibuteSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(__bridge NSString *)kUTTypeImage];
attibuteSet.title =[NSString stringWithFormat:#"%#",object]; //retrive title from object and add here
//attibuteSet.contentDescription = #"blah blah "; // retrieve description from object and add here
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:#"app name"
domainIdentifier:#"com.compont.appname"
attributeSet:attibuteSet];
[searchableItems addObject:item];
}
if (searchableItems) {
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(#"Search item indexed");
}
}];
}
}
I haven't ran and tested the code.
you are not looping through each key. Use the code provided in this question.
CoreSpotlight indexing
Try it on a device that supports indexing.
NOT iPhone 4/4s or iPad.

NSDictionary in NSMutableArray ios

I've a NSMutableArray array having NSDictionary keys as:
NSMutableArray *arr=#[#{#"A":#{#"user":#"obj1",#"friend":#"obj2"}}];
No I want to add objects to this NSMutableArray.
My Code:
for (int i = 0; i < 10; i++)
{
NSString *user = #"A"+i;
for(int i=0;i<50;i++) {
NSString *friend1 = #"B"+i;
NSString *friend2 = #"C"+i;
NSString *friend3 = #"D"+i;
}
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil];
NSString *y= [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"%#",y);
Desired Output:
{"A":[{"user":"A1","friend":"B1"},{"user":"A1","friend":"B2"},{"user":"A2","friend":"B1"}]}
How can I set my objects in order to achieve desired output.
NSString* singleObjectTemplate = [NSString stringWithFormat:#"{\"user\" : \"%#\",\"friend\" : \"%#\"}", user, friend];
NSString* validJsonTemplate = [NSString stringWithFormat:#"{\"A\":[%#]}", singleObjectTemplate];
Something like this. You can modify it as per your requirement to add multiple entries.
I hope this helps you in some way. Cheers!! :)
EDIT:
Suppose you want to add 3 objects to it.
NSString *str = #"";
for (int i = 0; i<3; i++)
{
NSString* singleObjectTemplate = [NSString stringWithFormat:#"{\"user\" : \"%#\",\"friend\" : \"%#\"}", user, friend];
str = [str stringByAppendingString:singleObjectTemplate];
if(i<2)
str = [str stringByAppendingString:#", "];
}
NSString* validJsonTemplate = [NSString stringWithFormat:#"{\"A\":[%#]}", str];

Objective-C Download PLIST and use within table

I'm fairly new to Objective-C programming. I'm trying to build an app for personal use to view event info which is contained on my web server. I wish for each Event Title string to be shown as a separate row within my UITableView.
I'm using CFPropertyList to create the plist from MySQL. Here's what my plist looks like:
I'm downloading the plist like so:
NSString *stringURL = #"http://www.example.com/events.plist";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"events.plist"];
[urlData writeToFile:filePath atomically:YES];
}
Then this is how I'm trying to load the info into their arrays:
events = [[NSDictionary alloc] initWithContentsOfFile:filePath];
ID = [events objectForKey:#"Event ID"];
title = [events objectForKey:#"Event Title"];
date = [events objectForKey:#"Event Date"];
price = [events objectForKey:#"Price"];
totalTickets = [events objectForKey:#"Total Tickets"];
ticketsSold = [events objectForKey:#"Tickets Sold"];
ticketsRemaining = [events objectForKey:#"Tickets Remaining"];
postStatus = [events objectForKey:#"Post Status"];
postContent = [events objectForKey:#"Post Content"];
Now here's where I'm having trouble. Now when I'm debugging the number of rows the 'events' dictionary has it returns 0.
Therefore I cannot set the 'numberOfRowsInSection'. Nor does [title objectAtIndex:indexPath.row] work.
I believe the issue lays with how I'm setting up the NSDictionary, but due to my lack of Objective-C and array knowledge, I'm unable to overcome this issue.
Here's your problem:
events = [[NSDictionary alloc] initWithContentsOfFile:filePath];
Your plist is an array of dictionaries and you're trying to load that array as a dictionary. Try this instead:
events = [NSArray arrayWithContentsOfFile:filePath];
Edit: To extract the titles into an array so you can use them in table cells, do this:
NSMutableArray *titles = [NSMutableArray array];
for (NSDictionary *event in events) {
NSString *title = [event objectForKey:#"Event Title"];
[titles addObject:title];
}

how to add NSString from loop in NSMutableArray

I want to add NSString Name in NSmutableArray but I dont know about that.
I get 5 NSString name from url in wamp server and I want add these names in NSMutableArray.
this is my code but do not work!!! :
NSMutableArray *file;
for (int j=0; j < 5; j++) {
NSString *fileName = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://192.168.1.101/janatan/filemanager.php?dir=root&file=%d&name",j]]];
NSLog(#"%#",fileName);
[file addObject:fileName] //right???
}
You are not allocating NSMutableArray
NSMutableArray *file = [[NSMutableArray alloc] initWithCapacity:5];
If you are not sure about the number of elements gets added to array beforehand, you can use
NSMutableArray *file = [[NSMutableArray alloc] init];
Firstly, the NSMutableArray must be allocated.
Secondly, the using of magic numbers must be avoided.
Next, the readability of code should be improved by replace
NSString *fileName = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://192.168.1.101/janatan/filemanager.php?dir=root&file=%d&name",j]]];
to something more convenient. Moreover, the memory here is allocated but never released.
Your code may be as follows:
const int numberOfFiles = 5;
NSMutableArray *file = [NSMutableArray array]
for(int i = 0; i < numberOfFiles; ++i){
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://192.168.1.101/janatan/filemanager.php?dir=root&file=%d&name", i]];
NSString *fileName = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
[file addObject:fileName];
}
But here are we can find some problems.
For example if url is changed on server-side you'd rewrite the code. If the number of elements is changed it's the same. So it would be good to find a way to avoid this kind of dependence.

How do I parse a NSString?

I have a string
https://gdata.youtube.com/feeds/api/videos?q=4s-PbMuNooo
I want to get string 4s-PbMuNooo. How do I parse a NSString?
Short answer :
NSString *myString = #"https://gdata.youtube.com/feeds/api/videos?q=4s-PbMuNooo";
NSArray *components = [myString componentsSeparatedByString:#"="];
NSString *query = [components lastObject];
Problems :
1) What if the bit after the q= contains another =
2) What if the q= bit is missing?
A better answer is for you to read the documentation - there are lots of helper methods on NSString that will get you substrings. Look for rangeOfString to find out where the equals would be and subStringWithRange to get the bit you want.
EDIT: Thomas has raised a fair point about URL parsing - see his answer here
A slightly longer but more complete answer. Hope this helps:
NSURL *url = [NSURL URLWithString: #"https://gdata.youtube.com/feeds/api/videos?param1=yeah&param2="];
NSArray *listItems = [[url query] componentsSeparatedByString:#"&"];
NSMutableDictionary *keyValues = [NSMutableDictionary dictionaryWithCapacity:listItems.count];
for (NSString *item in listItems) {
NSArray *keyValue = [item componentsSeparatedByString:#"="];
NSAssert(keyValue.count == 2, #"Key value pair mismatch");
[keyValues setObject:[keyValue objectAtIndex:1] forKey:[keyValue objectAtIndex:0]];
}
NSLog(#"1: %#", [keyValues objectForKey:#"param1"]);
NSLog(#"2: %#", [keyValues objectForKey:#"param2"]);
Like this:
NSArray *listItems = [yourString componentsSeparatedByString:#"="];
NSString *myFinalString=[NSString stringWithString:[listItems objectAtIndex:1]];
I wanted to try this a bit, so here is my code that handles more than one parameters:
NSURL *url = [NSURL URLWithString:#"https://gdata.youtube.com/feeds/api/videos?p=123123&q=234"];
NSArray *queryArray = [[url query] componentsSeparatedByString:#"&"];
for (NSString *queryString in queryArray) {
NSArray *queryComponents = [queryString componentsSeparatedByString:#"="];
if ([[queryComponents objectAtIndex:0] isEqualToString:#"q"]) {
NSLog(#"Found q: %#", [queryString substringFromIndex:2]);
} else {
NSLog(#"Did not find q.");
}
}
The question and its title are badly chosen - the answers are generally right for the more general task of splitting ANY string up, but bad for splitting up URLs as this question is actually about.
Here's how to properly get the values from a URL:
To break up a URL string, first do this:
NSURL *url = [NSURL URLWithString:urlString];
Then retrieve the parameters (the part past the "?") like this:
NSString *query = [url query];
Now you can go ahead and split that query string up using componentsSeparatedByString:#"&" as shown in the other answers.

Resources