简体   繁体   English

移动除第一行以外的所有行,然后添加新行 Google 电子表格 api Nodejs

[英]Shift all rows besides the first row and then add new row Google spreadsheet api Nodejs

I am using google spreadsheets api with nodejs to insert a row on the second row of one of the files.我正在使用 google 电子表格 api 和 nodejs 在其中一个文件的第二行插入一行。 So my idea was to create an empty row at the row2 and then add new row values.所以我的想法是在 row2 处创建一个空行,然后添加新的行值。

The problem is I want to shift all the rows besides the first one, and add a new empty row in the second row keeping all the data and then adding the new obtained data to the new created empty second row.问题是我想移动除第一行之外的所有行,并在第二行中添加一个新的空行以保留所有数据,然后将新获得的数据添加到新创建的空第二行中。

When I execute both requests like that below:当我执行如下两个请求时:

 let range = "MySheetName!A2";
 let rowArr = [
     [1,2,3,4,5,6,7,8],
 ];
this.insertSecondRowBlank();
this.exeGoogleBatchUpdate()
    .then(
        this.updateRowsOnSpreadsheet(range, rowArr)
    );

the row with values is added on the top of row2, so it is deleting the data existing there, and then it is shifting and adding the blanked row, and what I want is the opposite.有值的行被添加到 row2 的顶部,所以它正在删除那里存在的数据,然后它正在移动并添加空白行,而我想要的是相反的。 I want to keep the data in row2 and just shift all the rows besides row1 and then add the new row in a blank row just created.我想将数据保留在 row2 中,只移动 row1 以外的所有行,然后将新行添加到刚刚创建的空白行中。

The functions used here:这里用到的函数:

async updateRowsOnSpreadsheet(range, rowArr){
    const googleSpreadSheet = await this.getSheetInstance();
    const spreadSheetValues = googleSpreadSheet.spreadsheets.values;
    const updateRows = spreadSheetValues.update({
        auth: await this.getAuth(),
        spreadsheetId:this.fileID,
        range: range,
        valueInputOption: "RAW",
        resource:{
            values: rowArr,
        }
    });
}

async insertSecondRowBlank(sheetID = "0"){
    let singleRequest =  { 
        insertDimension : {
            range : {
                sheetId : sheetID,
                dimension : "ROWS", 
                startIndex : 1,  
                endIndex : 2,
            },
            inheritFromBefore : false,
        } 
    };
    this.requests.push(singleRequest);
}

async exeGoogleBatchUpdate(){
    let requests = this.requests;
    const batchUpdateRequest = {requests:requests};
    const sheets = await this.getSheetInstance();
    sheets.spreadsheets.batchUpdate({
        spreadsheetId:this.fileID,
        resource: batchUpdateRequest,
    },(err, response) => {
        if (err) {
        } else {
        }
    });
}

What should I do to make google accept the first request first and then accept the second request in order?我应该怎么做才能让谷歌先接受第一个请求,然后按顺序接受第二个请求? Because when I send, it is executing then updateRowsOnSpreadsheet first and then executing the insertSecondRowBlank even if the order I send is the opposite of that.因为当我发送时,它首先执行updateRowsOnSpreadsheet ,然后执行insertSecondRowBlank ,即使我发送的顺序与此相反。

I think that the methods of googleapis for Node.js return Promise. In your situation, how about the following modification?我认为 Node.js 的 googleapis 方法返回 Promise。在您的情况下,以下修改如何?

Modified script:修改脚本:

async updateRowsOnSpreadsheet(range, rowArr) {
  const googleSpreadSheet = await this.getSheetInstance();
  const spreadSheetValues = googleSpreadSheet.spreadsheets.values;
  const updateRows = await spreadSheetValues.update({
    auth: await this.getAuth(),
    spreadsheetId: this.fileID,
    range: range,
    valueInputOption: "RAW",
    resource: {
      values: rowArr,
    },
  });
  return updateRows;
}

insertSecondRowBlank(sheetID = "0") {
  let singleRequest = {
    insertDimension: {
      range: {
        sheetId: sheetID,
        dimension: "ROWS",
        startIndex: 1,
        endIndex: 2,
      },
      inheritFromBefore: false,
    },
  };
  this.requests.push(singleRequest);
}

async exeGoogleBatchUpdate() {
  let requests = this.requests;
  const batchUpdateRequest = { requests: requests };
  const sheets = await this.getSheetInstance();
  const res = await sheets.spreadsheets.batchUpdate({
    spreadsheetId: this.fileID,
    resource: batchUpdateRequest,
  });
  return res;
}

In the above modified script, please modify the script for executing these functions as follows.在上述修改后的脚本中,请将执行这些功能的脚本修改如下。

// Please modify the function name from `run` to your actual function name.
async run() {

  // Please declare "sheetName".

  let range = sheetName + "!A2";
  let rowArr = [[1, 2, 3, 4, 5, 6, 7, 8]];
  this.insertSecondRowBlank();
  const res1 = await this.exeGoogleBatchUpdate();
  const res2 = await this.updateRowsOnSpreadsheet(range, rowArr);
  console.log(res1.data);
  console.log(res2.data);
}
  • When I tested this, I confirmed that after a new row was inserted to the 2nd row, the value of rowArr was put to the inserted new row.当我测试这个时,我确认在第二行插入新行后, rowArr的值被放入插入的新行。
  • I thought that from exeGoogleBatchUpdate() , I thought that in your script, auth: await this.getAuth() in updateRowsOnSpreadsheet might not be required to be used.我认为从exeGoogleBatchUpdate() ,我认为在您的脚本中,可能不需要使用updateRowsOnSpreadsheet中的auth: await this.getAuth()

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将行添加到第一个空行? 带有 chrome 扩展程序的 Google 电子表格 API - How to add row to first empty row? Google spreadsheet API with chrome extensions 使用 javascript 在 Google 电子表格中添加行 - Add row in google Spreadsheet with javascript 电子表格中的颜色行(Google脚本) - Color row in spreadsheet (google script) 将谷歌电子表格逐行调整 - Adjust google spreadsheet row by one 在Java脚本的第一行之前添加新行 - Add new Row before first row in java script 计算所有行的行添加列 - Calculate column on row add for all rows 在 Google 可视化中添加新的数据行 - Add new Data Row in Google Visualisation 如何在所有行中隐藏 REACT 材料表上的列,并在我正在编辑的特定行的编辑上显示它,并在新行添加时添加操作? - How to hide a column on REACT Material table in all rows and show it on Edit for the Particular Row I am editing and Add Operations on new row adding? 如何使用上一行的所有元素添加新表行 - How to add new table row with all elements of previous row 在表中添加新行以使脚本不仅在第一行与新行中的元素连接时的方式是什么? - what is the way when add new row in table to make script connect with element in new row not only at first row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM