簡體   English   中英

表格 API v4 - 給定電子表格,創建具有自定義標題的表格

[英]Sheets API v4 - Creating a sheet with custom title, given a spreadsheet

我一直在嘗試這樣做,它創建了工作表,但沒有使用給定工作表的標題。

function addSheet($spreadsheet, $sheet_title) {
    $client = getClient();
    $service = new Google_Service_Sheets($client);
    
    $response = $service->spreadsheets->batchUpdate($spreadsheet->spreadsheetId, new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
        "requests" => [
            "addSheet" => new Google_Service_Sheets_AddSheetRequest([
                "properties" => new Google_Service_Sheets_SheetProperties([
                    "title" => $sheet_title
                ])
            ])
        ]
    ]));

    return $response;
}

它出什么問題了?

編輯:我試過了,它似乎正在使用以下內容:

function addSheet($spreadsheet, $sheet_title) {
    $client = getClient();
    $service = new Google_Service_Sheets($client);

    $request_body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
    $request_body->setRequests([
        "addSheet" => new Google_Service_Sheets_AddSheetRequest([
            "properties" => new Google_Service_Sheets_SheetProperties([
                "title" => $sheet_title
            ])
        ])
    ]);
    $response = $service->spreadsheets->batchUpdate($spreadsheet->spreadsheetId, $request_body);

    return $response;
}

但我仍然不明白另一個有什么問題......

如果您想在一行中完成所有操作,可以按以下方式進行:

$response = $service->spreadsheets->batchUpdate($spreadsheet->spreadsheetId, new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
        'requests' => array(
            'addSheet' => array(
                'properties' => array(
                    'title' => $sheet_title
                )
            )
        )
    )
  )
);

無需在Google_Service_Sheets_BatchUpdateSpreadsheetRequest中創建新對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM