[英]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。在您的情况下,以下修改如何?
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.rowArr
的值被放入插入的新行。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()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.