我有一个加油脚本可以将一系列单元格从一张纸复制到另一张单元格正确插入的数据,但也添加了两个额外的空白行
我的用户可以在单独的纸上输入新产品,以供买家审核。如果被接受,则买家然后从菜单上运行脚本。
该过程正常工作,将新信息复制到价格簿中,然后从新项目表中清除数据。但是,即使这两张纸中没有额外的行,该过程似乎总是添加两个空白单元格。
function addNewItems() {
//Get new item data from New Items Sheet
var ss = SpreadsheetApp.getActive();
var s = ss.getSheetByName("New Items");
s.activate();
//define non-contiguous data ranges from source sheet fpr clearing after copying
var newInput1 = ss.getRange("New Items!A2:K");
var newInput2 = ss.getRange("New Items!N2:O");
var newInput3 = ss.getRange("New Items!Q2:R");
// Get last row of data from source sheet
lastR = s.getLastRow();
// Select range of data with new item information (17 is the last column of data)
var newItems = s.getRange(2,1,lastR,17).getValues();
//Switch to target sheet to add new records into Price Book
var ts = ss.getSheetByName("Price Book");
ts.activate();
//Find last row of Price Book data and add one to get next row
tsIns = ts.getLastRow();
//Start at the next row and insert the data range from the New Item sheet
var newInsert = ts.getRange(tsIns +1,1,lastR,17).setValues(newItems);
//Clear data from new item sheets after copying data to the Price Book
newInput1.clearContent();
newInput2.clearContent();
newInput3.clearContent();
}
My users can enter new products on a separate sheet for review from the buyer. If accepted, the buyer then runs the script from the menu.
The process works perfectly, copying the new information into the price book and then clearing the data from the New Item sheet. But, the process seems to always add two blank cells, even though there are no extra rows in either sheet.
function addNewItems() {
//Get new item data from New Items Sheet
var ss = SpreadsheetApp.getActive();
var s = ss.getSheetByName("New Items");
s.activate();
//define non-contiguous data ranges from source sheet fpr clearing after copying
var newInput1 = ss.getRange("New Items!A2:K");
var newInput2 = ss.getRange("New Items!N2:O");
var newInput3 = ss.getRange("New Items!Q2:R");
// Get last row of data from source sheet
lastR = s.getLastRow();
// Select range of data with new item information (17 is the last column of data)
var newItems = s.getRange(2,1,lastR,17).getValues();
//Switch to target sheet to add new records into Price Book
var ts = ss.getSheetByName("Price Book");
ts.activate();
//Find last row of Price Book data and add one to get next row
tsIns = ts.getLastRow();
//Start at the next row and insert the data range from the New Item sheet
var newInsert = ts.getRange(tsIns +1,1,lastR,17).setValues(newItems);
//Clear data from new item sheets after copying data to the Price Book
newInput1.clearContent();
newInput2.clearContent();
newInput3.clearContent();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下操作:
Try this:
我不知道2,但肯定1。在GetRange中,第三个参数是要获得或设置的行数。由于您从第2行开始getValues(),因此您应该从LASTR中减去1,因为其值是带有包括标题在内的值的行总数。
因此,如图所示,您应该更改以下2行代码。
I don't know about 2, but definitely 1. In getRange, the third parameter is number of rows to get or set. Since you getValues() starting at row 2 you should subtract 1 from lastR since its value is the total number of rows with values including the headers.
Therefore you should change the following 2 lines of code as shown.