CSV upload (convert to spreadsheet) to google drive in iOS?

£可爱£侵袭症+ 提交于 2019-12-24 05:02:51

问题


I have generate csv file to store inside local directory path. i have tried to upload csv file to google drive, it working fine. open a csv file from google drive not showing preview in spreadsheet. so i want to upload csv file (convert csv to spreadsheet) to google drive. How to convert this?

Thanks in Advance

Images from google drive

Example for upload csv file to drive for your reference:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *root = [documentsDir stringByAppendingPathComponent:@"Report.csv"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:root];
if(fileExists == YES)
{
        GTLUploadParameters *uploadParameters = nil;
        self.driveFile = [[GTLDriveFile alloc]init];

        NSData *dat = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:root]];
        uploadParameters =
        [GTLUploadParameters uploadParametersWithData:dat MIMEType:@"text/csv"];

        self.driveFile.title = @"Report.csv";
        GTLQueryDrive *query = nil;
        if (self.driveFile.identifier == nil || self.driveFile.identifier.length == 0)
        {
            query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile
                                                uploadParameters:uploadParameters];
        }
        else
        {
            query = [GTLQueryDrive queryForFilesUpdateWithObject:self.driveFile
                                                          fileId:self.driveFile.identifier
                                                uploadParameters:uploadParameters];
        }
        UIAlertView *alert = [DrEditUtilities showLoadingMessageWithTitle:@"While data is uploading..."
                                                                 delegate:self];

        [self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                                  GTLDriveFile *updatedFile,
                                                                  NSError *error)
         {
            appDel.File_Id = updatedFile.identifier;
            [alert dismissWithClickedButtonIndex:0 animated:YES];
            if (error == nil)
            {
                self.driveFile = updatedFile;

                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Feel Fix" message:@"Successfully Uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
                [alert show];
            }
            else
            {
                NSLog(@"An error occurred: %@", error);
                [DrEditUtilities showErrorMessageWithTitle:@"Unable to save file"
                                                   message:[error description]
                                                  delegate:self];
            }
        }];

回答1:


Insert Convert property to fix this issue

    GTLUploadParameters *uploadParameters = nil;
    self.driveFile = [[GTLDriveFile alloc]init];

    NSData *dat = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:root]];
    uploadParameters =
    [GTLUploadParameters uploadParametersWithData:dat MIMEType:@"text/csv"];

    self.driveFile.title = @"Report.csv";
    GTLQueryDrive *query = nil;
    if (self.driveFile.identifier == nil || self.driveFile.identifier.length == 0)
    {
        query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile
                                            uploadParameters:uploadParameters];

        query.convert = YES;   // Convert file format to spread sheet

    }
    else
    {
        query = [GTLQueryDrive queryForFilesUpdateWithObject:self.driveFile
                                                      fileId:self.driveFile.identifier
                                            uploadParameters:uploadParameters];
    }
    UIAlertView *alert = [DrEditUtilities showLoadingMessageWithTitle:@"While data is uploading..."
                                                             delegate:self];

    [self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                              GTLDriveFile *updatedFile,
                                                              NSError *error)
     {
        appDel.File_Id = updatedFile.identifier;
        [alert dismissWithClickedButtonIndex:0 animated:YES];
        if (error == nil)
        {
            self.driveFile = updatedFile;

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Feel Fix" message:@"Successfully Uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
            [alert show];
        }
        else
        {
            NSLog(@"An error occurred: %@", error);
            [DrEditUtilities showErrorMessageWithTitle:@"Unable to save file"
                                               message:[error description]
                                              delegate:self];
        }
    }];


来源:https://stackoverflow.com/questions/22247341/csv-upload-convert-to-spreadsheet-to-google-drive-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!