简体   繁体   English

Google Sheets + Apps Scripts,从一个工作表复制/粘贴到另一个工作表,但粘贴到特定列 (B) 中的第一个空单元格

[英]Google Sheets + Apps Scripts, Copy/Paste from one sheet to another, but Paste into first empty cell in specific column (B)

  • I am trying to write a script that will copy data from a range on one sheet and paste to another in the first empty cell in column B. I have a script that can paste it into the first empty cell in the sheet and that works fine, but I have a static row of ID numbers in column A, so when it pastes the data it is way down the sheet.我正在尝试编写一个脚本,该脚本将从一个工作表上的范围复制数据并粘贴到 B 列第一个空单元格中的另一个。我有一个脚本可以将其粘贴到工作表中的第一个空单元格中,并且工作正常,但我在 A 列中有一行静态 ID 号,因此当它粘贴数据时,它会在工作表的下方。 So, I need a way to skip over column A and paste into the first empty cell in column B. Hope that makes sense.所以,我需要一种方法来跳过 A 列并粘贴到 B 列的第一个空单元格中。希望这是有道理的。

  • The other part is there will be empty cells in column B sometimes.另一部分是有时B列中会有空单元格。 So it needs to skip over those to get the very last empty cell.所以它需要跳过那些以获得最后一个空单元格。

  • I found a tutorial to get that last empty cell and have that working fine, but can't figure out how to incorporate it into a function that will paste the data.我找到了一个教程来获取最后一个空单元格并使其正常工作,但无法弄清楚如何将其合并到将粘贴数据的函数中。 Here is what I have so far:这是我到目前为止所拥有的:

 function myFunction() { var sss = SpreadsheetApp.openById('1dp5s8G9vFF5LzF5mAaY-JoySLGekh5UKHwt6jFcGOnA'); var ss = sss.getSheetByName('Form'); var range = ss.getRange('A8:H'); var data = range.getValues(); var tss = SpreadsheetApp.openById('1Vq_xX9cbC6OBAh1IRKsVKSPQvc5FEUFsJ2mlPzuFZ6o'); var ts = tss.getSheetByName('Data'); var columnToCheck = ts.getRange("B:B").getValues(); var lastRow = getLastRowSpecial(columnToCheck); Logger.log(lastRow) }; function getLastRowSpecial(range){ var rowNum = 0; var blank = false; for(var row = 0; row < range.length; row++){ if(range[row][0] === "" && !blank){ rowNum = row; blank = true; }else if(range[row][0] !== ""){ blank = false; }; }; return rowNum; };

  • So basically I need a function that will setValues(data) at the cell determined by "lastRow"所以基本上我需要一个函数来设置由“lastRow”确定的单元格的值(数据)

  • Alternatively, if anyone has a suggestion on how this may be accomplished a different way, I would love to hear it.或者,如果有人对如何以不同的方式完成这件事有任何建议,我很想听听。

Explanation:解释:

  • You are very close.你很亲近。 The getLastRowSpecial function indeed gets the last row of column B and it takes into account the blank cells. getLastRowSpecial函数确实获取了B列的最后一行,并考虑了空白单元格。

  • Your goal is then to use the returned value lastRow and set the data starting from lastRow+1 by using setValues .然后,您的目标是使用返回值lastRow并使用setValues设置从lastRow+1开始的数据。

     ts.getRange(lastRow+1,2,data.length,data[0].length).setValues(data);
  • Another modification I made in your script was instead of taking the full data range, I get only until the last row with content.我在您的脚本中所做的另一个修改是,我没有获取完整的data范围,而是只获取包含内容的最后一行。 If this does not work for you then skip that part and use your solution.如果这对您不起作用,请跳过该部分并使用您的解决方案。 This is the part I modified:这是我修改的部分:

     var range = ss.getRange('A8:H'+ss.getLastRow());

Solution:解决方案:

I only modified myFunction :我只修改了myFunction

function myFunction() {
  var sss = SpreadsheetApp.openById('1dp5s8G9vFF5LzF5mAaY-JoySLGekh5UKHwt6jFcGOnA');
  
  var ss = sss.getSheetByName('Form'); 
  var range = ss.getRange('A8:H'+ss.getLastRow()); // new code
  var data = range.getValues();
 
     var tss = SpreadsheetApp.openById('1Vq_xX9cbC6OBAh1IRKsVKSPQvc5FEUFsJ2mlPzuFZ6o');
     var ts = tss.getSheetByName('Data');
     var columnToCheck = ts.getRange("B:B").getValues();
     var lastRow = getLastRowSpecial(columnToCheck);
     
     ts.getRange(lastRow+1,2,data.length,data[0].length).setValues(data); // new code

};
 
function getLastRowSpecial(range){
  var rowNum = 0;
  var blank = false;
  for(var row = 0; row < range.length; row++){
 
    if(range[row][0] === "" && !blank){
      rowNum = row;
      blank = true;
    }else if(range[row][0] !== ""){
      blank = false;
    };
  };
  return rowNum;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将单元格复制并粘贴到特定列谷歌表格中的第一个空行 - Copy and Paste a Cell to First Empty Row in Specific Column google sheets 从Google驱动器中可用的所有工作表中复制特定的单元格值,然后使用应用脚本粘贴到主工作表中 - Copy specific cell value from all sheets which are available in the google drive and paste in master sheet with apps script Google表格:根据复选框从一张纸复制并粘贴到另一张纸 - Google Sheets: Copy and Paste from one sheet to another based on checkbox Google 表格脚本 - 将粘贴值从一列复制到另一列 - Google sheets script - copy paste values from one column to another 谷歌表格复制一个单元格并粘贴到另一个单元格 - Google Sheets Copy one Cell and paste into another Cell Google Sheets 将行复制并粘贴到另一个工作表中,然后从第一个工作表中删除内容 - Google Sheets copy and paste row into another sheet and then delete content from the first sheet 从母版工作表的最后一行复制数据,并将其粘贴到Google工作表中另一工作表的特定单元格 - Copy data from last row of master sheet and paste it in specific cells in another sheet in google sheets Google Apps 脚本:将选定的单元格复制并粘贴到另一个工作表,然后从源中删除 - Google Apps Script: Copy and paste selected cell to another sheet, then delete from source 如何使用 Google Sheets Macros 从一张工作表复制值并将它们粘贴到另一张工作表中? - How to copy values from one sheet and paste them into another using Google Sheets Macros? 将单元格值从一张纸复制并粘贴到另一张onEdit - copy and paste cell value from one sheet to another onEdit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM