[英]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.或者,如果有人对如何以不同的方式完成这件事有任何建议,我很想听听。
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());
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.