c# - Google Sheets API CopyPasteRequest 无效
问题描述
我只是想将电子表格中第一张工作表中的一系列单元格复制到第二张工作表中。此代码运行没有错误,但实际工作表上没有任何反应。
我在下面缺少什么?
var spreadSheet = service.Spreadsheets.Get(spreadsheetId).Execute();
int? sourceSheetId = spreadSheet.Sheets[0].Properties.SheetId;
int? targetSheetId = spreadSheet.Sheets[1].Properties.SheetId;
// Copy in the current data from the "Training Data" tab
CopyPasteRequest copyReq = new CopyPasteRequest()
{
Source = new GridRange() { SheetId = sourceSheetId, StartColumnIndex = 0,
StartRowIndex = 1, EndColumnIndex = 0, EndRowIndex = 3000 },
Destination = new GridRange() { SheetId = targetSheetId,
StartColumnIndex = 0, StartRowIndex = 1, EndColumnIndex = 0, EndRowIndex = 3000 },
PasteType = "PASTE_VALUES",
PasteOrientation = "NORMAL"
};
var copyResource = new BatchUpdateSpreadsheetRequest() { Requests = new List<Request>() };
var reqCopy = new Request() { CopyPaste = copyReq };
copyResource.Requests.Add(reqCopy);
var result = service.Spreadsheets.BatchUpdate(copyResource, spreadsheetId).Execute();
// no errors, and result object is populated -- but nothing appears in the target sheet
解决方案
根据我的理解,您正在尝试将源工作表的 A 列复制到目标工作表的 A 列。
您的请求没有效果的原因是您EndColumnIndex
在GridRange中不正确,EndColumnIndex
并且EndRowIndex
被排除在要复制的范围内。
endRowIndex
范围的结束行(不包括),如果无界则不设置。
结束列索引
范围的结束列(不包括),如果无界则不设置。
例子:
我想复制Sheet1!A1:A10
到Sheet2!A1:A10
请求正文:
{
"requests": [
{
"copyPaste": {
"source": {
"sheetId": 0,
"startRowIndex": 0,
"startColumnIndex": 0,
"endColumnIndex": 1,
"endRowIndex": 10
},
"destination": {
"sheetId": 133812xxxx,
"startRowIndex": 0,
"startColumnIndex": 0,
"endColumnIndex": 1,
"endRowIndex": 10
},
"pasteOrientation": "NORMAL",
"pasteType": "PASTE_NORMAL"
}
}
]
}
- 工作表索引从零开始
endColumnIndex
设置为 1(不包括 B 列)endRowIndex
设置为 10(第 11 行除外)
输出:
推荐阅读
- angular - 如何将输入传递给 Angular 组件的 SCSS?
- javascript - 尝试使用 HTML & CSS & JS 制作灯泡
- android - 在 4.12.0 版本上滑动错误 .bitmapTransform
- python - 如何使用此代码通过使用 after 命令多次直接更改我的图像
- html - Css 网格,对齐内容:结束,在我的代码中不起作用
- qt - QTimer:定时器回调可以重叠执行吗?
- netflix-zuul - 如果请求正文小于 Content-Length 标头中指定的大小并使用 tomcat APR 连接器,则可能会发生这种情况
- javascript - Bean 名称“广告”的 BindingResult 和普通目标对象都不能用作请求属性
- java - Sphere-Sphere Intersection,选择正确的 theta
- python - 跳过列表中的空值