需要在Google表格中特定位置插入行

4 投票
2 回答
1591 浏览
提问于 2025-04-17 12:23

我正在使用Python来更新一个谷歌表格。谷歌的Python库可以让你通过InsertRow这个接口往表格里插入一行数据。

下面是一个例子:

gd_client.InsertRow(myDict, spreadSheetKey, workSheetKey)

这里的myDictionary是你要插入的数据字典,spreadsheetKey是你表格的关键字,而worksheetKey是你工作表的关键字。不过,我想在表格中间的某一特定行插入数据。这个接口只能在最后插入。

有没有人知道,有没有办法做到这一点?

谢谢。

2 个回答

1

如何通过API/OAuth以* 插入一行 *的方式在谷歌表格中间添加一行......这是一种比较复杂的方法。(我认为没有简单的方法,因为已经有人请求将这个功能添加到API中。)

          Pattern cellRefPattern = Pattern.compile("R(\\[?)([-0-9]+)\\]?C(\\[?)([-0-9]*)\\]?");
              worksheet.setRowCount(worksheet.getRowCount()+1);
              worksheet.update();
              CellFeed batchRequest = new CellFeed();
              for (AppCell cellAddr : cellAddresses.values()) {

                  // create a copy of the cell to replace
                  CellEntry batchEntry = new CellEntry(cellAddr.row, cellAddr.col, cellAddr.reference);

                  String updateReference = cellAddr.inputValue; 

                  if(updateReference.startsWith("=")) {
                      String removeReferenceBug = updateReference.replace( (CharSequence) "C:R", (CharSequence) "C[0]:R");
                      Matcher referenceMatcher = cellRefPattern.matcher(removeReferenceBug);
                      StringBuffer restultBuffer = new StringBuffer();
                      while (referenceMatcher.find()) {
                          try {
                              if(referenceMatcher.group(1).equals("[")) {
                                  int rowOffset = Integer.parseInt(referenceMatcher.group(2));
                                  int topRowOfSpan;
                                  int bottomRowOfSpan;                                
                                  int incSize = 1;
                                  if(rowOffset > 0) {
                                      topRowOfSpan = cellAddr.row;
                                      bottomRowOfSpan = cellAddr.row + rowOffset;
                                  } else {
                                      topRowOfSpan = cellAddr.row + rowOffset;
                                      bottomRowOfSpan = cellAddr.row ;      
                                      incSize = -1;
                                  }                               
                                  //System.out.println("move down: reference:"+cellAddr.reference+" topRowOfSpan:"+topRowOfSpan+
                                    //    " insertLocationRow:"+insertLocationRow+" bottomRowOfSpan:"+bottomRowOfSpan);
                                  if(topRowOfSpan <= insertLocationRow && bottomRowOfSpan > insertLocationRow) rowOffset += incSize;
                                  if(referenceMatcher.group(3).equals("[")) {
                                      referenceMatcher.appendReplacement(restultBuffer, "R["+rowOffset+"]C["+referenceMatcher.group(4)+"]");                                      

                                  } else {
                                      int colOffset = 0;                                          
                                      String col = referenceMatcher.group(4);                                         
                                      if(col != null && "".equals(col) == false) {
                                          colOffset = Integer.parseInt(col) - cellAddr.col;
                                      }                                       
                                      referenceMatcher.appendReplacement(restultBuffer, "R["+rowOffset+"]C["+colOffset+"]");                                                                              
                                  }                                   
                              } else {
                                  int absoluteRow = Integer.parseInt(referenceMatcher.group(2));
                                  if(absoluteRow >= insertLocationRow ) absoluteRow ++;
                                  if(referenceMatcher.group(3).equals("[")) {
                                      referenceMatcher.appendReplacement(restultBuffer, "R"+absoluteRow+"C["+referenceMatcher.group(4)+"]");                                                                                                              
                                  } else {
                                      referenceMatcher.appendReplacement(restultBuffer, "R"+absoluteRow+"C"+referenceMatcher.group(4));                                                                                                                                                   
                                  }

                              }
                          } catch(NumberFormatException nfe) {}
                      }
                      referenceMatcher.appendTail(restultBuffer);
                      updateReference = restultBuffer.toString();                                                                         

                  }


                  batchEntry.setId(String.format("%s/%s", worksheet.getCellFeedUrl().toString(), cellAddr.reference));              
                  batchEntry.changeInputValueLocal(updateReference);
                  BatchUtils.setBatchId(batchEntry, cellAddr.reference);
                  BatchUtils.setBatchOperationType(batchEntry, BatchOperationType.UPDATE);

                  // add the copy to the batch list
                  batchRequest.getEntries().add(batchEntry);
              }


              // Submit the update
              Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
              service.setHeader("If-Match", "*");
              CellFeed batchResponse = service.batch(new URL(batchLink.getHref()), batchRequest);
              service.setHeader("If-Match", null);

这是对https://developers.google.com/google-apps/spreadsheets/#updating_multiple_cells_with_a_batch_request的修改版本

问题:没有检查公式中的字符串

AppCell包含:行、列和引用

4

一个简单的方法是在同一个表格里准备两个工作表。一个是原始数据,直接从外部导入的;另一个是计算后的数据(比如排序、函数等),是基于原始数据进行处理的。

比如,如果你想按第二列来排序数据,你可以在第二个工作表里这样做:

=EXPAND(SORT(Sheet1!A:D,2,1))

这样分开做也很好,因为如果你想从外部更新数据,就不会和你对数据进行的操作发生冲突,比如在数据行中添加计算列。

撰写回答