Google 工作表 API CopyPasteRequest 无效

Google Sheets API CopyPasteRequest has no effect

我只是想将一系列单元格从我的传播sheet中的第一个sheet复制到第二个。此代码运行没有错误,但在实际 sheet.

上没有任何反应

我在下面遗漏了什么?

 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

根据我的理解,您正在尝试将源 sheet 的 A 列复制到目标 sheet 的 A 列。

之所以你的请求没有效果是因为你在GridRange中的EndColumnIndex不正确,EndColumnIndexEndRowIndex被排除在范围内被复制。

endRowIndex

The end row (exclusive) of the range, or not set if unbounded.

endColumnIndex

The end column (exclusive) of the range, or not set if unbounded.


示例:

我要复制Sheet1!A1:A10Sheet2!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"
      }
    }
  ]
}
  • sheet 索引是从零开始的
  • endColumnIndex设置为1(B列除外)
  • endRowIndex 设置为 10(排除第 11 行)

输出: