1

I am creating a file on the fly in Google Drive using PHP and Google Drive API. The api allows you to add data while you are creating it , I know I can use spreadsheet API to add data later, but I want to add data on the fly , Here is my code

$service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setTitle( 'Hello world!' );
$file->setDescription('A test document');
$data = "Header1,Header2,Header2,"; // CSV
$file->setMimeType( 'application/vnd.google-apps.spreadsheet' );
    $file = $service->files->insert( $file , array(
  'data' => $data,
  'convert' => true, ));

Here is my code, that successfully creates the file with title and description but doesn't add the headers, am I missing something ?

5
  • You probably forgot something or where are the $service defined? Commented Aug 1, 2016 at 18:33
  • @RogerWayne I just pasted the snippet, since the file is being created fine so assume that the $service is defined with no issue, I will update though. Commented Aug 1, 2016 at 18:37
  • Do you mean writing on spreadsheet cells using PHP? Commented Aug 3, 2016 at 0:08
  • @noogui yes ofcourse, how else we can add data to spreadsheet other than writing to cells. Commented Aug 3, 2016 at 15:51
  • @noogui yes, it is resoved Commented Sep 9, 2016 at 20:06

1 Answer 1

1

First setup the PHP Quickstart for Sheets API. It includes the authentication and authorization processes you need. Then checkout the guide on Writing on Sheets API.

PUT https://sheets.googleapis.com/v4/spreadsheets/spreadsheet_id/values/range?valueInputOption=valueInputOption

When I wrote on a spreadsheet, I used XHR requests in JS. It's up to you to do it in PHP, might be easier. My xhr request body looked like:

         var params = {
           "range":"Sheet1!A1",
           "majorDimension": "ROWS",
           "values": [
           ["This is a test"]
          ],
         }

Just take note of your spreadsheetId and sheetId. You can find the sheetId of the open sheet in the spreadsheet URL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.