我已经试图解决/寻找解决这个问题已经有一段时间了。 我已阅读过gspread的文档,但未找到重命名工作表的方法。 你们当中有人知道吗?我会非常感激! 确实有worksheet.title可以获取工作表的名称,但我找不到重命名实际表格的方法。
提前感谢您!
我已经试图解决/寻找解决这个问题已经有一段时间了。 我已阅读过gspread的文档,但未找到重命名工作表的方法。 你们当中有人知道吗?我会非常感激! 确实有worksheet.title可以获取工作表的名称,但我找不到重命名实际表格的方法。
提前感谢您!
这是我个人编写的一个库的提取:
def _batch(self, requests):
body = {
'requests': requests
}
return self._service.spreadsheets().batchUpdate(spreadsheetId=self.spreadsheetId, body=body).execute()
def renameSheet(self, sheetId, newName):
return self._batch({
"updateSheetProperties": {
"properties": {
"sheetId": sheetId,
"title": newName,
},
"fields": "title",
}
})
我认为只要稍加努力,你就可以将其实现到你的代码中,并获得你想要的结果。
为了进行batchUpdate
调用,你需要获取spreadsheetId以及根据Python Quickstart - Google Sheet API中所述初始化的service
。
batchupdate UpdateSheetProperties
API调用一次性修改多个属性,请查看JSON请求结构:添加您的“properties”,并在“fields”字符串内容中逗号分隔它们的名称。FieldMasks格式很复杂,但我在这里找到了一个很好的解释。 - abuupdate_title
方法就像简单的操作一样。import gspread
# authenticate:
gc = gspread.service_account()
# choose a gspread method to open your spreadsheet and your worksheet:
wsh = gc.open_by_key(spreadsheetId).worksheet("old_WorkSheetName")
# update your worksheet name:
wsh.update_title("new_WorksheetName")
就是这样。
您可以通过Python发送HTTP请求来解决您的问题。
链接在这里
您需要通过HTTP发送一些工作表的元数据。
例如,使用Python获取工作表的ID,并发送以下信息:
<entry>
<id>
https://spreadsheets.google.com/feeds/worksheets/key/private/full/worksheetId
</id>
<updated>2007-07-30T18:51:30.666Z</updated>
<category scheme="http://schemas.google.com/spreadsheets/2006"
term="http://schemas.google.com/spreadsheets/2006#worksheet"/>
<title type="text">Income</title>
<content type="text">Expenses</content>
<link rel="http://schemas.google.com/spreadsheets/2006#listfeed"
type="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full"/>
<link rel="http://schemas.google.com/spreadsheets/2006#cellsfeed"
type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/key/worksheetId/private/full"/>
<link rel="self" type="application/atom+xml"
href="https://spreadsheets.google.com/feeds/worksheets/key/private/full/worksheetId"/>
<link rel="edit" type="application/atom+xml"
href="https://spreadsheets.google.com/feeds/worksheets/key/private/full/worksheetId/version"/>
<gs:rowCount>45</gs:rowCount>
<gs:colCount>15</gs:colCount>
</entry>
该网站还拥有Java和.NET解决方案。(这是针对旧版本3的)
对于较新的版本,您也可以使用来自Python的POST http请求进行批量更新。
链接在这里
请求的数据为:
{
"requests": [{
"updateSpreadsheetProperties": {
"properties": {"title": "My New Title"},
"fields": "title"
}
}]
}
需要通过POST发送到https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate
在这两个请求中,将URL中的spreadsheetId替换为您正在编辑的Google表格的ID。
请注意URL中从v3更改为v4。
如果您正在使用版本3应用程序并希望迁移,请单击此处
一位评论者指出第二个请求不会更改工作表的名称。我添加的链接显示了更改工作表的复杂属性的方法,我将很快更新我的答案。
{"requests": [{"updateSheetProperties": {"properties": {"sheetId": "My Sheet ID", "title": "My New Title"}, "fields": "title"}}]}
;http地址保持不变。 - Lukeimport pygsheets
gc = pygsheets.authorize()
# open spreadsheet and then worksheet
sh = gc.open('my new spreadsheet')
wks = sh.sheet1
wks.title = 'new title'
sheetsService = getSheetsService();
// create a SheetProperty object and put there all your parameters (new title, sheet id, something else)
SheetProperties title = new SheetProperties().setSheetId(0).setTitle("Main");
// make a request with this properties
UpdateSheetPropertiesRequest rename = new UpdateSheetPropertiesRequest().setProperties(title);
// set fields you want to update
rename.setFields("title");
// as requestBody.setRequests gets a list, you need to compose an list from your request
List<Request> requests = new ArrayList<>();
// convert to Request
Request request = new Request().setUpdateSheetProperties(rename);
requests.add(request);
BatchUpdateSpreadsheetRequest requestBody = new BatchUpdateSpreadsheetRequest();
requestBody.setRequests(requests);
// now you can execute batchUpdate with your sheetsService and SHEET_ID
sheetsService.spreadsheets().batchUpdate(SHEET_ID, requestBody).execute();
这里可以找到有关sheetProperties的更多信息。
import {google} from 'googleapis';
const auth = new google.auth.OAuth2(...)
const sheetsService = google.sheets({version: 'v4', auth})
const requests = [
{
updateSheetProperties: {
properties: {
sheetId: 'id-of-the-sheet-that-you-want-to-rename',
title: 'new-title',
},
fields: 'title'
}
}
];
sheetsService.spreadsheets.batchUpdate({
spreadsheetId: 'some-spreasheet-id',
requestBody: {
requests,
},
});
使用PHP
public function updateSheetTitle(string $fileId, string $title, ?int $sheetId = NULL): void
{
if (!$sheetId) {
// rename first sheet
$file = $this->get($fileId);
if (!$file->getSheets()) {
throw new NoSheetsException();
}
$sheetId = $file->getSheets()[0]->getProperties()->getSheetId();
}
$titleProp = new Google_Service_Sheets_SheetProperties();
$titleProp->setSheetId($sheetId);
$titleProp->setTitle($title);
$renameReq = new Google_Service_Sheets_UpdateSheetPropertiesRequest();
$renameReq->setProperties($titleProp);
$renameReq->setFields('title');
$request = new Google_Service_Sheets_Request();
$request->setUpdateSheetProperties($renameReq);
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$batchUpdateRequest->setRequests([$request]);
$this->spreadsheets->batchUpdate($fileId, $batchUpdateRequest);
}
UpdateSheetPropertiesRequest
的fields
参数,它告诉API请求期间应该编辑什么内容。在下面的示例中,sheetId
仅用于更新标题。我不知道如果将fields
参数更改为“sheetId”,那么该请求是否能够更新sheetId(例如)。 - Mattia Galatiworksheet.update_title(title)
,但现在不能用了。 - manas