簡體   English   中英

Google Sheets Api:復制谷歌電子表格

[英]Google Sheets Api: Copy google spreadsheet

試圖復制整個電子表格,但我想沒有 api 可以這樣做。

基本上,我正在嘗試執行以下操作:

  • 有一個電子表格,我想復制它並稍作改動。
  • 創建一個新的電子表格,將模板中的所有工作表一張一張復制到新的電子表格中(電子表格復制會更有效率)

創建新的電子表格工作正常,但是從電子表格復制工作表不起作用。

嘗試了2種方法:

角度:

$http.post("https://sheets.googleapis.com/v4/spreadsheets/"+fromSpreadsheetId+"/sheets/"+fromSheetId,
                            {headers: {
                        'Authorization': 'Bearer ' + this.oauthToken
                     }},

給出以下錯誤:

對預檢請求的響應未通過訪問控制檢查:沒有“Access-Control-Allow-Origin”

谷歌表格 API 調用:

gapi.client.sheets.spreadsheets.sheets.copyTo({spreadsheetId: fromSpreadsheetId , sheetId: fromSheetId},{destinationSpreadsheetId: destinationSpreadsheetId});

請求通過沒有任何錯誤。 但是,新創建的電子表格沒有復制工作表。

您可能想專門針對 CORS 問題提出一個單獨的問題,因為這是一個單獨的問題。

關於“復制電子表格”,您有兩種選擇:

1) 使用 Drive API 的files.copy方法。 Drive API 中的fileId等同於 Sheets API 中的spreadsheetId ID。

2) 不要使用您復制的“模板”電子表格。 相反,請使用 Sheet API 的電子表格.create方法。 您可以使用電子表格.get來檢索您的“模板”JSON,並可以在創建新電子表格之前根據需要對其進行調整。

我也遇到過這個問題,所以正如之前寫的那樣,最好的也是唯一正確的方法是使用 Drive API 的 files.copy 方法。 這是 PHP 示例,您可以如何做到這一點:

function copySpreadSheet(string $spreadsheetId, string $title, string email) {
    $serviceSheets = new Google_Service_Sheets(getGoogleClient());
    $serviceDrive = new Google_Service_Drive(getGoogleClient());

    $fileCopy = $serviceDrive->files->copy($spreadsheetId, new Google_Service_Drive_DriveFile());
    insertPermission($serviceDrive, $fileCopy->getId(), $email, 'user', 'owner');
    $requests = [
        'updateSpreadsheetProperties' => [
            'properties' => [
                'title'         => $title,
            ],
            'fields' => 'title'
        ]
    ];
    $requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
        'requests' => $requests,
    ]);
    $response = $serviceSheets->spreadsheets->batchUpdate($fileCopy->getId(), $requestBody);

    return $serviceSheets->spreadsheets->get($fileCopy->getId());
}

如果有必要,這里是insertPermission()代碼:

function insertPermission(Google_Service_Drive $service, string $fileId, string $email, string $type, string $role) {
    $newPermission = new Google_Service_Drive_Permission([
        'type' => $type,
        'role' => $role,
        'emailAddress' => $email,
    ]);
    return $service->permissions->create($fileId, $newPermission, [
            'fields' => 'id',
            'transferOwnership' => 'true',
        ]
    );
}

這個問題很老,但是談到最新的Sheets API v4 ,您需要requestBody字段才能復制工作表。

sheets.spreadsheets.sheets.copyTo({
    spreadsheetId: fromSpreadsheetId,
    sheetId: fromSheetId,
    requestBody: {
        destinationSpreadsheetId: destinationSpreadsheetId
    }
})

檢查文檔,但請注意,在他們的示例中,他們使用resource而不是requestBody 我正在使用 TypeScript,但使用resource時出現類型錯誤

這是一個更改文檔示例的 TypeScript 示例,其中我在同一個電子表格中復制了一張工作表(對於復制,也可以使用 batchUpdate 進行復制請求)。

 const request: sheets_v4.Params$Resource$Spreadsheets$Sheets$Copyto = { // The ID of the spreadsheet containing the sheet to copy. spreadsheetId: '9MhcPzrWE-Pv9MKhJwow6cWi9uO46RcDTfxhvT9X1fY', // The ID of the sheet to copy. sheetId: 3, requestBody: { // The ID of the spreadsheet to copy the sheet to. destinationSpreadsheetId: '9MhcPzrWE-Pv9MKhJwow6cWi9uO46RcDTfxhvT9X1fY' } }; try { const response = (await this.sheets.spreadsheets.sheets.copyTo(request)).data; console.log(JSON.stringify(response, null, 2)); } catch (err) { console.error(err); }

我使用了上面兩個答案的組合:

實際上,我做了以下事情:

使用此功能復制電子表格`

function copySpreadSheet(string $spreadsheetId, string $title, string $email) {
        $serviceSheets = new Google_Service_Sheets($this->getClient());
        $serviceDrive = new Google_Service_Drive($this->getClient());

        $fileCopy = $serviceDrive->files->copy($spreadsheetId, new Google_Service_Drive_DriveFile());

        $requests = [
            'updateSpreadsheetProperties' => [
                'properties' => [
                    'title'         => $title,
                ],
                'fields' => 'title'
            ]
        ];
        $requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
            'requests' => $requests,
        ]);
        $response = $serviceSheets->spreadsheets->batchUpdate($fileCopy->getId(), $requestBody);

        return $serviceSheets->spreadsheets->get($fileCopy->getId());
    }

然后使用此代碼段將所有權更改為我

$drive = new Google_Service_Drive($client);

$newPermission = new Google_Service_Drive_Permission();

$newPermission->setEmailAddress('EMAIL@WHATE.COM');

$newPermission->setType('group');

$newPermission->setRole('writer');

$response = $drive->permissions->create($newSpreadsheetId, $newPermission);

暫無
暫無

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

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