我有一个加油脚本可以将一系列单元格从一张纸复制到另一张单元格正确插入的数据,但也添加了两个额外的空白行

发布于 2025-02-12 11:51:30 字数 1351 浏览 3 评论 0原文

我的用户可以在单独的纸上输入新产品,以供买家审核。如果被接受,则买家然后从菜单上运行脚本。

该过程正常工作,将新信息复制到价格簿中,然后从新项目表中清除数据。但是,即使这两张纸中没有额外的行,该过程似乎总是添加两个空白单元格。

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神仙妹妹 2025-02-19 11:51:30

尝试以下操作:

function addNewItems() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName("New Items");
  let lastR = sh.getLastRow();
  var newInput1 = sh.getRange("A2:K" + lastR);
  var newInput2 = sh.getRange("N2:O" + lastR);
  var newInput3 = sh.getRange("Q2:R" + lastR);
  var newItems = sh.getRange(2, 1, lastR - 1, 17).getValues();
  var tsh = ss.getSheetByName("Price Book");
  tsIns = tsh.getLastRow();
  tsh.getRange(tsIns + 1, 1, lastR - 1, 17).setValues(newItems);
  newInput1.clearContent();
  newInput2.clearContent();
  newInput3.clearContent();
}

Try this:

function addNewItems() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName("New Items");
  let lastR = sh.getLastRow();
  var newInput1 = sh.getRange("A2:K" + lastR);
  var newInput2 = sh.getRange("N2:O" + lastR);
  var newInput3 = sh.getRange("Q2:R" + lastR);
  var newItems = sh.getRange(2, 1, lastR - 1, 17).getValues();
  var tsh = ss.getSheetByName("Price Book");
  tsIns = tsh.getLastRow();
  tsh.getRange(tsIns + 1, 1, lastR - 1, 17).setValues(newItems);
  newInput1.clearContent();
  newInput2.clearContent();
  newInput3.clearContent();
}
笑梦风尘 2025-02-19 11:51:30

我不知道2,但肯定1。在GetRange中,第三个参数是要获得或设置的行数。由于您从第2行开始getValues(),因此您应该从LASTR中减去1,因为其值是带有包括标题在内的值的行总数。

因此,如图所示,您应该更改以下2行代码。

var newItems = s.getRange(2,1,lastR-1,17).getValues();

var newInsert = ts.getRange(tsIns +1,1,lastR-1,17).setValues(newItems);

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.

var newItems = s.getRange(2,1,lastR-1,17).getValues();

var newInsert = ts.getRange(tsIns +1,1,lastR-1,17).setValues(newItems);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文