Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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. So my idea was to create an empty row at the row2 and then add new row values.

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. 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.

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think that the methods of googleapis for Node.js return Promise. In your situation, how about the following modification?

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.
  • I thought that from exeGoogleBatchUpdate(), I thought that in your script, auth: await this.getAuth() in updateRowsOnSpreadsheet might not be required to be used.

Reference:

  • Google APIs Node.js Client
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!