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.
I think that the methods of googleapis for Node.js return Promise. In your situation, how about the following modification?
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);
}
rowArr was put to the inserted new row.exeGoogleBatchUpdate(), I thought that in your script, auth: await this.getAuth() in updateRowsOnSpreadsheet might not be required to be used.