需要在指定位置向谷歌电子表格插入行。

4

我正在使用Python更新谷歌电子表格。谷歌针对电子表格的Python库允许您使用InsertRow API将行插入到电子表格中。

以下是一个示例:

gd_client.InsertRow(myDict, spreadSheetKey, workSheetKey)

myDictionary是要插入的字典,spreadsheetKey是你的电子表格的键,worksheetKey是你的工作表的键。

然而,我想在我的电子表格中间特定的行中进行插入。这个API只能在末尾进行插入。

有人知道如何解决这个问题吗?

谢谢。

2个回答

4

一种简单的方法是在同一个电子表格中拥有两个工作表。一个是原始数据,它从外部插入,另一个是基于该原始数据计算出来的(排序、函数等等)。

例如,如果您想按第二列对数据进行排序,可以将其放在第二个工作表中:

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

如果你想从外部更新数据,这种分离也很好。这将与您对数据执行的操作产生冲突,如向数据行添加计算列。


谢谢您的建议。不完全是我想要的。要弄清楚我的电子表格的布局需要很多工作; 我没有相关功能可以这样做。我在考虑,也许我必须自己在内存中排序所有内容,创建一个新的工作表,然后再次添加所有内容。我希望我不必这样做。 - dublintech
我已经更新了答案,包括第二个工作表的示例,基本上是对原始“Sheet1”数据进行排序。 - Guy

1
如何通过API/OAuth在Google电子表格中“插入一行”到中间位置……这是一种较为困难的方法。(我认为没有简单的方法,因为有一个功能请求将其添加到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 包含:行、列和引用


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接