1
votes

I`d like to create spreadsheet from .csv file. Code:

static void Main()
        {
            String CLIENT_ID = "<MY ID>";
            String CLIENT_SECRET = "<MY SECRET>";

            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
            var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
            var service = new DriveService(auth);

            File body = new File();
            body.Title = "My spread";
            body.Description = "A test spread";
            body.MimeType = "application/vnd.google-apps.spreadsheet";

            byte[] byteArray = System.IO.File.ReadAllBytes("spread.csv");
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

            service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet").Upload();
        }

        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            string[] scopes = new string[] { "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/userinfo.profile" };
            IAuthorizationState state = new AuthorizationState(scopes);
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();

            // Retrieve the access token by using the authorization code:
            return arg.ProcessUserAuthorization(authCode, state);
        }

After inserting file and clicking on it on google-drive page(logged as owner) I get message

"We're sorry. The spreadsheet at this URL could not be found. Make sure that you have the right URL and that the owner of the spreadsheet hasn't deleted it"

When I change MimeType to "text/csv" and insert file, after clicking on it, I get message "No preview available This item was created with (MyApp'sName), a Google Drive app. Download this file or use one of the apps you have installed to open it."

I can also right click on this file(that one created with "text/csv" MimeType) and choose option "Export to Google Docs" and this gives me result that I'd like to reach - spreadsheet file with my .csv's file content. But such indirect method doesn`t fully satify me. Is there any method to make spreadsheet file on google drive, with content from .csv file direct from my application?

2
any final solution with full source code sample ?Kiquenet

2 Answers

1
votes

I don't know c#, but on the basis it mirrors Java, within the line

service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet").Upload();

you need to insert the equivalent of

.setConvert(true)

also ...

the mimetype should be "text/csv"

1
votes

I've found the answer :)

How to programmatically convert a file into an appropriate Google Documents format:

var service = new DriveService(new BaseClientService.Initializer
{
    ...
});

...

FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, _mimeType);
request.Convert = true;
request.Upload();