PHP Google Sheets API v4 从前一行获取所有格式并应用于新行插入的最有效方法?

插入新数据行并应用前一行样式的最佳方法是什么?(我们正在迁移我们的 PHP 代码以使用 Google Sheets api v4 类)

我目前正在解决这个问题,并针对电子表格使用 Google_Service_Sheets_BatchUpdateSpreadsheetRequest->batchUpdate 我可以在代码中应用格式更改。但是,我不太确定是否可以循环使用适当的获取请求以获取现有格式并使用这些格式?

在旧货币中,我们根本不必担心这一点,因为我们只是将新行的数据添加到 listFeed->insert 函数中,该函数似乎处理了格式化方面本身。

但是现在的问题是我是否需要循环遍历范围内的每个单元格以获取格式,然后一次将该单元格应用于新行,还是有更好的方法(例如现有的行级操作)?



月关宝盒
浏览 74回答 1
1回答

慕妹3242003

我通常不会回答我自己的问题,但鉴于 Google 将于 2020 年 3 月关闭 v3 Sheets API,我怀疑其他人可能会遇到这个问题。正如我在对原始问题的评论中提到的那样,v4 API 中没有访问单元格格式的现有方法。因此,没有选项可以遍历一个范围并将格式应用于另一个范围。我的解决方案是复制/粘贴前一行(默认 PASTE_NORMAL 将复制所有值、公式、格式并合并https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request# pastetype ) 然后用所需的值覆盖新粘贴的行。这为我提供了新行中现有格式所需的值:-&nbsp; &nbsp; $sheetId = null;&nbsp; &nbsp; $worksheetSheets = $this->spreadsheetService->spreadsheets->get($this->spreadsheetId)->sheets;&nbsp; &nbsp; foreach($worksheetSheets as $sheet){&nbsp; &nbsp; &nbsp; &nbsp; $sheetTitle = $sheet->properties['title'];&nbsp; &nbsp; &nbsp; &nbsp; if ($sheetTitle === $this->worksheetTitle){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sheetId = $sheet->properties['sheetId'];&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $copyRange = new Google_Service_Sheets_GridRange();&nbsp; &nbsp; $copyRange->setSheetId($sheetId);&nbsp; &nbsp; $copyRange->setStartRowIndex($nextRow - 2);&nbsp; &nbsp; $copyRange->setEndRowIndex($nextRow - 1);&nbsp; &nbsp; $copyRange->setStartColumnIndex(0);&nbsp; &nbsp; $copyRange->setEndColumnIndex(400);&nbsp; &nbsp; $pasteRange = new Google_Service_Sheets_GridRange();&nbsp; &nbsp; $pasteRange->setSheetId($sheetId);&nbsp; &nbsp; $pasteRange->setStartRowIndex($nextRow - 1);&nbsp; &nbsp; $pasteRange->setEndRowIndex($nextRow);&nbsp; &nbsp; $pasteRange->setStartColumnIndex(0);&nbsp; &nbsp; $pasteRange->setEndColumnIndex(400);&nbsp; &nbsp; $copypasteRequest = new Google_Service_Sheets_CopyPasteRequest();&nbsp; &nbsp; $copypasteRequest->setSource($copyRange);&nbsp; &nbsp; $copypasteRequest->setDestination($pasteRange);&nbsp; &nbsp; //$copypasteRequest->pasteType(CopyPasteType.PASTE_NORMAL);&nbsp; &nbsp; $request = new Google_Service_Sheets_Request();&nbsp;&nbsp; &nbsp; $request-> setCopyPaste($copypasteRequest);&nbsp; &nbsp; $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();&nbsp; &nbsp; $batchUpdateRequest->setRequests($request);&nbsp; &nbsp; // Need to check if sheet has an existing empty row to paste into&nbsp; &nbsp; $finalRowRange = $this->worksheet['range'];&nbsp; &nbsp; $finalRowStartPosition = strpos($this->worksheet['range'],':') + 2;&nbsp; &nbsp; $finalRow = intval(substr($finalRowRange,$finalRowStartPosition));&nbsp; &nbsp; if($finalRow <= $pasteRange['startRowIndex']){ // startRowIndex is a zero based array range, i.e. 348 actually corresponds to row 349 on sheet.&nbsp; &nbsp; &nbsp; &nbsp; $appendDimensionRequest = new Google_Service_Sheets_AppendDimensionRequest();&nbsp; &nbsp; &nbsp; &nbsp; $appendDimensionRequest->setSheetId($sheetId);&nbsp; &nbsp; &nbsp; &nbsp; $appendDimensionRequest->setDimension("ROWS");&nbsp; &nbsp; &nbsp; &nbsp; $appendDimensionRequest->setLength(1);&nbsp; &nbsp; &nbsp; &nbsp; $appendRequest = new Google_Service_Sheets_Request();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $appendRequest->setAppendDimension($appendDimensionRequest);&nbsp; &nbsp; &nbsp; &nbsp; $appendEmptyRowBatchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();&nbsp; &nbsp; &nbsp; &nbsp; $appendEmptyRowBatchUpdateRequest->setRequests($appendRequest);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this->spreadsheetService->spreadsheets->batchUpdate($this->spreadsheetId, $appendEmptyRowBatchUpdateRequest);&nbsp; &nbsp; }$this->spreadsheetService->spreadsheets->batchUpdate($this->spreadsheetId, $batchUpdateRequest);&nbsp; &nbsp; // The actual data values insert&nbsp; &nbsp; $this->spreadsheetService->spreadsheets_values->update(&nbsp; &nbsp; &nbsp; &nbsp;$this->spreadsheetId,&nbsp; &nbsp; &nbsp; &nbsp;$updateRange,&nbsp; &nbsp; &nbsp; &nbsp;$valueRange,&nbsp; &nbsp; &nbsp; &nbsp;$conf&nbsp; &nbsp; );我希望这可能对将来的某些人有用。
打开App,查看更多内容
随时随地看视频慕课网APP