[英]Google Sheets Scripts onOpen(e) Add New Row to Sheet
I have a custom function that inserts a new row under the last non-empty row in a sheet and copies a specific range to this new row. 我有一个自定义函数,可以在工作表中的最后一个非空行下插入新行,并将特定范围复制到该新行。 This functions works fine when I trigger it with a custom menu item.
当我使用自定义菜单项触发它时,此功能可以正常工作。
However, when I add this custom function to the onOpen(e) trigger the script does not work. 但是,当我将此自定义函数添加到onOpen(e)触发器时,脚本不起作用。 I troubleshooted and added an alert in my function - the alert triggers so therefore the function is read in the right place.
我对功能进行了故障排除并添加了警报-警报会触发,因此可以在正确的位置读取该功能。
Please advise how to have my script work onOpen? 请告知我的脚本如何在OpenOpen上运行?
Here's my code for my custom function: 这是我的自定义函数的代码:
function onOpen(e) {
autoAddRow();
}
function autoAddRow() {
var updateFrequency = 3;
var lRow = getLastRow('A');
var lDate = all.getRange(lRow,1).getValue();
var today = new Date();
Date.prototype.addDays = function(days) {
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}
var newDate = lDate.addDays(updateFrequency);
if (today >= newDate) {
var lCol = all.getLastColumn();
var src = all.getRange(3,1,1,6);
var dest = all.getRange(lRow+1,1,1,6);
if(lRow > 20) {
all.insertRowsAfter(lRow, 1);
}
src.copyTo(dest, {contentsOnly:false});
} else {
return;
}
}
So if your example isn't psuedo-code, it wont work. 因此,如果您的示例不是伪代码,它将无法正常工作。 It dies at the second line of your function because the method isn't attached to a class.
它死在函数的第二行,因为该方法未附加到类。
Here is my version of your code (I'm using the first sheet in the spreadsheet app). 这是我的代码版本(我正在使用电子表格应用程序中的第一张纸)。
function autoAddRow() {
var updateFrequency = 3
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var data = sheet.getDataRange();
var lRow = data.getLastRow();
var lDate = new Date(sheet.getRange(lRow,1).getValue());
var today = new Date();
Logger.log(lDate);
Logger.log(lRow);
Logger.log(sheet.getRange(lRow,1).getValue());
Date.prototype.addDays = function(days) {
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}
var newDate = lDate.addDays(updateFrequency);
if (today >= newDate) {
Logger.log("passed if statement");
var lCol = data.getLastColumn();
var src = sheet.getRange(lRow,1,1,6);
var dest = sheet.getRange(lRow+1,1,1,6);
if(lRow > 20) {
all.insertRowsAfter(lRow, 1);
}
src.copyTo(dest, {contentsOnly:false});
} else {
return;
}
}
The main problems were in the calls to the range and data. 主要问题在于调用范围和数据。 They didn't go anywhere.
他们什么都没去。 The variable lDate needed to be parsed as a date (in my version, my date column was the second column, not the first *edit: changed the code quick to point to your original date cell *end edit), but you have to call the range and get the value to parse it, but after fixing the class issues with regards to getting your data, the rest fell into place.
需要将变量lDate解析为日期(在我的版本中,我的日期列是第二列,而不是第一列* edit:将代码快速更改为指向原始日期单元格* end edit),但是您必须调用范围并获得解析的值,但是在解决了有关获取数据的类问题之后,其余的就到位了。 It even works if you put in in the onOpen.
如果您放入onOpen,它甚至可以工作。
Update: I didn't fix the line 更新:我没有解决这个问题
if(lRow > 20) {
all.insertRowsAfter(lRow, 1);
}
In fact, I'm not really sure what it does because it looks like it just inserts the row after the last row, which the rest of the function does. 实际上,我不太确定它的作用,因为看起来它只是在最后一行之后插入了该行,其余的功能都这样做了。 But the code 'as is' will break after 20 lines unless you change 'all' to reference the worksheet object.
但是代码“原样”将在20行后中断,除非您更改“全部”以引用工作表对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.