Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda