在以星期日为日期的日期后插入空白行 google 应用程序脚本
Insert blank row after the date that has Sunday as the day google app scripts
我在 google 工作表中有一列包含日期范围。
我想在恰好是星期天的日期之后插入一个空行(我只关心这一天)。
我想到的伪代码如下:
- 遍历日期列
- 格式化日期,以便我们只获取完整缩写的日期(即星期日)
- 如果格式化日期等于字符串“Sunday”,则在该日期下方插入一行。
这是我当前的脚本:
function addRows() {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getActiveSheet();
const range = sheet.getDataRange();
const data = range.getValues();
for (let i = 1; i < data.length; i++) {
const sheetDate = data[i][0];
const day = Utilities.formatDate(sheetDate, ss.getSpreadsheetTimeZone(), "EEEE");
if (day === "Sunday") {
sheet.insertRowsAfter(i, 1); // I know this shouldn't be i, it should be the index of the row with the value "Sunday"
}
}
}
我认为问题出在 insertRowsAfter()
函数的第一个参数上。这个参数应该是什么?
It seems that every insertion of new rows after a row position, your data range adjusts, thus, you also need to adjust the row position for the next succeeding rows that contains Sunday
.
建议
或许您可以试试下面这个经过调整的脚本:
function addRows() {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getActiveSheet();
const range = sheet.getDataRange();
const data = range.getValues();
var count = 0;
for (let i = 1; i < data.length; i++) {
var row = i+1; //Get the sheet row number of every data from "data" variable
const sheetDate = data[i][0];
const day = Utilities.formatDate(sheetDate, ss.getSpreadsheetTimeZone(), "EEEE");
if (day == "Sunday") {
count += 1; //Count how many times "Sunday" was found
var curRow = row+count; //Adjust the current row #
curRow -= 1; //Subtract 1 row from the current row to match the sheet
sheet.insertRowsAfter(curRow,1)
}
}
}
样本测试
- 测试Sheet:
- 在 运行 调整后的脚本之后:
我在 google 工作表中有一列包含日期范围。 我想在恰好是星期天的日期之后插入一个空行(我只关心这一天)。
我想到的伪代码如下:
- 遍历日期列
- 格式化日期,以便我们只获取完整缩写的日期(即星期日)
- 如果格式化日期等于字符串“Sunday”,则在该日期下方插入一行。
这是我当前的脚本:
function addRows() {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getActiveSheet();
const range = sheet.getDataRange();
const data = range.getValues();
for (let i = 1; i < data.length; i++) {
const sheetDate = data[i][0];
const day = Utilities.formatDate(sheetDate, ss.getSpreadsheetTimeZone(), "EEEE");
if (day === "Sunday") {
sheet.insertRowsAfter(i, 1); // I know this shouldn't be i, it should be the index of the row with the value "Sunday"
}
}
}
我认为问题出在 insertRowsAfter()
函数的第一个参数上。这个参数应该是什么?
It seems that every insertion of new rows after a row position, your data range adjusts, thus, you also need to adjust the row position for the next succeeding rows that contains
Sunday
.
建议
或许您可以试试下面这个经过调整的脚本:
function addRows() {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getActiveSheet();
const range = sheet.getDataRange();
const data = range.getValues();
var count = 0;
for (let i = 1; i < data.length; i++) {
var row = i+1; //Get the sheet row number of every data from "data" variable
const sheetDate = data[i][0];
const day = Utilities.formatDate(sheetDate, ss.getSpreadsheetTimeZone(), "EEEE");
if (day == "Sunday") {
count += 1; //Count how many times "Sunday" was found
var curRow = row+count; //Adjust the current row #
curRow -= 1; //Subtract 1 row from the current row to match the sheet
sheet.insertRowsAfter(curRow,1)
}
}
}
样本测试
- 测试Sheet:
- 在 运行 调整后的脚本之后: