OStack程序员社区-中国程序员成长平台

标题: ios - CSV 上传(转换为电子表格)到 iOS 中的谷歌驱动器? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:50
标题: ios - CSV 上传(转换为电子表格)到 iOS 中的谷歌驱动器?

我已生成 csv 文件以存储在本地目录路径中。我试图将 csv 文件上传到谷歌驱动器,它工作正常。从谷歌驱动器打开一个 csv 文件,而不是在电子表格中显示预览。所以我想将 csv 文件(将 csv 转换为电子表格)上传到谷歌驱动器。 如何转换?

提前致谢

来自谷歌驱动器的图像

enter image description here

上传 csv 文件到驱动器供您引用的示例:

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];
            }
        }];



Best Answer-推荐答案


插入 Convert 属性来解决这个问题

    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];
        }
    }];

关于ios - CSV 上传(转换为电子表格)到 iOS 中的谷歌驱动器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22247341/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4