0
votes

I'm trying to create a spreadsheet which have 4 sheets and some format. I read the document of Google and knew that we can use method spreadsheets.create to do this. I made a json request body to test this API and it work but I don't know how to send this json in PHP. This is my json string, you can test it in the Google Spreadsheet API document: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create

{
  "properties": {
    "title": "Dự toán",
    "defaultFormat": {
      "verticalAlignment": "MIDDLE",
      "wrapStrategy": "WRAP",
      "textFormat": {
        "fontFamily": "Arial",
        "fontSize": 12
      }
    }
  },
  "sheets": [
    {
      "properties": {
        "sheetId": 0,
        "index": 0,
        "title": "CP Xây lắp"
      },
      "merges": [
        {
          "sheetId": 0,
          "startColumnIndex": 0,
          "endColumnIndex": 5,
          "startRowIndex": 0,
          "endRowIndex": 1
        },
        {
          "sheetId": 0,
          "startColumnIndex": 0,
          "endColumnIndex": 5,
          "startRowIndex": 2,
          "endRowIndex": 3
        },
        {
          "sheetId": 0,
          "startColumnIndex": 0,
          "endColumnIndex": 5,
          "startRowIndex": 3,
          "endRowIndex": 4
        },
        {
          "sheetId": 0,
          "startColumnIndex": 0,
          "endColumnIndex": 5,
          "startRowIndex": 4,
          "endRowIndex": 5
        }
      ],
      "data": [
        {
          "startRow": 0,
          "startColumn": 0,
          "rowData": [
            {
              "values": [
                {
                  "userEnteredValue": {
                    "stringValue": "BẢNG TỔNG HỢP CHI PHÍ XÂY LẮP"
                  },
                  "userEnteredFormat": {
                    "horizontalAlignment": "CENTER"
                  }
                }
              ]
            },
            {
              "values": [
                {}
              ]
            },
            {
              "values": [
                {
                  "userEnteredValue": {
                    "stringValue": "CÔNG TRÌNH: "
                  },
                  "userEnteredFormat": {
                    "horizontalAlignment": "CENTER",
                    "textFormat": {
                      "bold": true
                    }
                  }
                }
              ]
            },
            {
              "values": [
                {
                  "userEnteredValue": {
                    "stringValue": "HẠNG MỤC: "
                  },
                  "userEnteredFormat": {
                    "horizontalAlignment": "CENTER",
                    "textFormat": {
                      "bold": true
                    }
                  }
                }
              ]
            },
            {
              "values": [
                {
                  "userEnteredValue": {
                    "stringValue": "ĐỊA ĐIỂM: "
                  },
                  "userEnteredFormat": {
                    "horizontalAlignment": "CENTER",
                    "textFormat": {
                      "bold": true
                    }
                  }
                }
              ]
            }
          ],
          "rowMetadata": [
            {}
          ],
          "columnMetadata": [
            {}
          ]
        }
      ]
    }
  ]
}

Could somebody help me please ^^

1
You need to show some PHP code that you already have written when trying to initiate this request. - PhistucK
I found the solution and answer it below. Thank you very much :D - Sinh

1 Answers

0
votes

I've done with it. First just create a blank spreadsheet. Then decode the json request string. We can create the json request follow this document: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate

Then we use $service->spreadsheets->batchUpdate to send the request.

$requests = json_decode($json_request_string);

$requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$requestBody->setRequests($requests);

$response = $service->spreadsheets->batchUpdate($spreadsheetId, $requestBody);