【问题标题】:Need to insert row into Google spreadhseet at specific location需要在特定位置将行插入 Google 电子表格
【发布时间】:2012-02-05 21:52:47
【问题描述】:

我正在使用 python 来更新一个谷歌电子表格。用于电子表格的 google python 库允许您使用 InsertRow api 在电子表格中插入一行。

这是一个例子:

gd_client.InsertRow(myDict, spreadSheetKey, workSheetKey)

myDictionary 是您要插入的字典, spreadsheetKey 是电子表格的键,worksheetKey 是工作表的键。 但是,我想插入电子表格中间的特定行。此 API 仅在末尾插入。

谁知道,有什么办法吗?

谢谢。

【问题讨论】:

    标签: python google-sheets google-spreadsheet-api


    【解决方案1】:

    一种简单的方法是在同一个电子表格中放置 两张 工作表。一个是从外部插入的原始数据,第二个是基于该原始数据计算得出的数据(排序、函数...)。

    例如,如果您希望数据按第二列排序,则可以放入第二张表中:

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

    这种分离也很好,就好像你想从外部更新数据一样。它将与您可能对数据执行的操作发生冲突,例如将计算列添加到数据行。

    【讨论】:

    • 感谢您的建议。不完全是我想要的。弄清楚我的电子表格的布局将是很多工作;我没有这样做的功能。我想我可能必须自己对内存中的所有内容进行排序,创建一个新工作表,然后再次添加所有内容。我希望我不必那样做。
    • 我已经更新了答案以包含第二张表的示例,该示例基本上是对原始“Sheet1”的数据进行排序
    【解决方案2】:

    如何* 通过 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 包含:行、列和引用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 2012-08-12
      相关资源
      最近更新 更多