使用API和java在google电子表格中插入一行

使用API和java在google电子表格中插入一行,java,insert,gdata,google-spreadsheet-api,Java,Insert,Gdata,Google Spreadsheet Api,我有一个20行的谷歌电子表格。如何在第10行下方插入新行 我可以使用以下命令删除行: URL listFeedUrl = worksheet.getListFeedUrl(); ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class); ListEntry row = listFeed.getEntries().get(10);

我有一个20行的谷歌电子表格。如何在第10行下方插入新行

我可以使用以下命令删除行:

            URL listFeedUrl = worksheet.getListFeedUrl();
            ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);

            ListEntry row = listFeed.getEntries().get(10);
            row.delete();
我可以通过重新调整工作表的大小在末尾添加一行。请看这里:

我想要一个如何在表中间插入一行的例子。 API中没有“插入”。解决方案是手动向下移动行以创建间隙。这不会移动粗体等“样式”。一些“有效”单元格引用在向下移动时会导致错误,需要更改为绝对引用

代码 在插入行或删除行后更新引用。这是最复杂的一点。 还有更多,但不容易分开

    /**
     * @param locationRow
     */
    private void updateSheetReferences(int locationRow, boolean insertingNewRow) {
        System.out.printf("\n%s ms elapsed updateSheetReferences \n", System.currentTimeMillis() - startTime);
        Pattern cellRefPattern = Pattern.compile("R(\\[?)([-0-9]+)\\]?C(\\[?)([-0-9]*)\\]?");
          int incDirection = 1;
          if (insertingNewRow == false)  incDirection = -1;
          for(AppCell nextCell : activeWorksheetCells.getAllCells()) {
              int row = nextCell.row;
              int col = nextCell.col;
              String cellInputValue = nextCell.inputValue;
              if (cellInputValue == null) continue;


              // create a copy of the cell to replace
              String updateReference = cellInputValue; 

              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*incDirection;
                              // the location of the deleted row is relative, and so one row lower if row was deleted
                              int locationDeletedOffset = 0;
                              if(row >= locationRow && insertingNewRow == false) locationDeletedOffset = -1; 

                              // get the top and bottom rows of the 
                              if(rowOffset > 0) {
                                  topRowOfSpan = row;
                                  bottomRowOfSpan = row + rowOffset;
                              } else {
                                  topRowOfSpan = row + rowOffset;
                                  bottomRowOfSpan = row ;       
                                  incSize = -1*incDirection;
                              }                               
                              //System.out.println("move down: reference:"+cellAddr.reference+" topRowOfSpan:"+topRowOfSpan+
                              //      " insertLocationRow:"+insertLocationRow+" bottomRowOfSpan:"+bottomRowOfSpan);
                              // IF reference is the deleted row 
                              if(insertingNewRow == false && row + rowOffset == locationRow+locationDeletedOffset) {
                                  referenceMatcher.appendReplacement(restultBuffer, "{}");                                                                                                                    
                              } else {
                                  if(topRowOfSpan <= locationRow+locationDeletedOffset && bottomRowOfSpan >= locationRow+locationDeletedOffset) rowOffset += incSize;
                                  if(referenceMatcher.group(3).equals("[")) {
                                      referenceMatcher.appendReplacement(restultBuffer, "R["+rowOffset+"]C["+referenceMatcher.group(4)+"]");                                      

                                  } else {
                                      int colOffset = 0;                                          
                                      String colText = referenceMatcher.group(4);                                         
                                      if(colText != null && "".equals(colText) == false) {
                                          colOffset = Integer.parseInt(colText) - col;
                                      }                                       
                                      referenceMatcher.appendReplacement(restultBuffer, "R["+rowOffset+"]C["+colOffset+"]");                                                                              
                                  }                                   
                              }
                          } else {
                              int absoluteRow = Integer.parseInt(referenceMatcher.group(2));
                              // IF reference is the deleted row
                              if(insertingNewRow == false && absoluteRow == locationRow) {
                                  referenceMatcher.appendReplacement(restultBuffer, "{}");                                                                                                                    
                              } else {
                                  if(absoluteRow >= locationRow ) absoluteRow += 1 * incDirection;
                                  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) {}
                  } // END while
                  referenceMatcher.appendTail(restultBuffer);
                  updateReference = restultBuffer.toString();                                                                         

              } // END IF

              nextCell.inputValue =  updateReference;
          }
    }
/**
*@param locationRow
*/
私有void updateSheetReferences(int locationRow,boolean insertingNewRow){
System.out.printf(“\n%s毫秒经过的更新sheetreferences\n”,System.currentTimeMillis()-startTime);
Pattern cellRefPattern=Pattern.compile(“R(\\[?)([-0-9]+)\\]?C(\\[?)([-0-9]*)\\]?”;
int incDirection=1;
如果(insertingNewRow==false)incDirection=-1;
对于(AppCell-nextCell:activeWorksheetCells.getAllCells()){
int row=nextCell.row;
int col=nextCell.col;
字符串cellInputValue=nextCell.inputValue;
如果(cellInputValue==null)继续;
//创建要替换的单元格的副本
字符串updateReference=cellInputValue;
if(updateReference.startsWith(“=”){
String removeReferenceBug=updateReference.replace((CharSequence)“C:R”,(CharSequence)“C[0:R”);
Matcher referenceMatcher=cellRefPattern.Matcher(removeReferenceBug);
StringBuffer ResultBuffer=新的StringBuffer();
while(referenceMatcher.find()){
试一试{
if(referenceMatcher.group(1).equals(“[”){
int rowOffset=Integer.parseInt(referenceMatcher.group(2));
int-topRowOfSpan;
int-bottomRowOfSpan;
int incSize=1*incDirection;
//删除行的位置是相对的,因此,如果删除了行,则会降低一行
int locationDeletedOffset=0;
如果(row>=locationRow&&insertingNewRow==false)locationDeletedOffset=-1;
//获取文件的顶部和底部行
如果(行偏移>0){
topRowOfSpan=行;
bottomRowOfSpan=行+行偏移量;
}否则{
topRowOfSpan=行+行偏移量;
bottomRowOfSpan=行;
incSize=-1*incDirection;
}                               
//System.out.println(“下移:参考:”+cellAddr.reference+“topRowOfSpan:”+topRowOfSpan+
//“insertLocationRow:”+insertLocationRow+“bottomRowOfSpan:“+bottomRowOfSpan”);
//如果引用是已删除的行
if(insertingNewRow==false&&row+rowOffset==locationRow+locationDeletedOffset){
appendReplacement(resultbuffer,{});
}否则{
如果(topRowOfSpan=locationRow+locationDeletedOffset)rowOffset+=incSize;
if(referenceMatcher.group(3).equals(“[”){
referenceMatcher.appendReplacement(ResultBuffer,“R[”+rowOffset+“]C[”+referenceMatcher.group(4)+“]);
}否则{
int colOffset=0;
String colText=referenceMatcher.group(4);
if(colText!=null&“.equals(colText)==false){
colOffset=Integer.parseInt(colText)-col;
}                                       
referenceMatcher.appendReplacement(ResultBuffer,“R[”+rowOffset+“]C[“+colOffset+”]);
}                                   
}
}否则{
int absoluteRow=Integer.parseInt(referenceMatcher.group(2));
//如果引用是已删除的行
if(insertingNewRow==false&&absoluteRow==locationRow){
appendReplacement(resultbuffer,{});
}否则{
如果(绝对行>=位置行)绝对行+=1*in方向;
if(referenceMatcher.group(3).equals(“[”){
referenceMatcher.appendReplacement(ResultBuffer,“R”+绝对行+“C[”+referenceMatcher.group(4)+“]);
}否则{