[英]Copy and Paste a Cell to First Empty Row in Specific Column google sheets
I am trying to copy and paste values of cell J3 from Activity Report to the first empty cell in column L.我正在尝试将单元格 J3 的值从活动报告复制并粘贴到列 L 中的第一个空单元格。
The cell to copy is fixed, but the cell to paste in is variable.要复制的单元格是固定的,但要粘贴的单元格是可变的。
So far the code I am using has only been able to copy the values from J3 to the last row of column L instead of the first blank row from the top.到目前为止,我使用的代码只能将值从 J3 复制到 L 列的最后一行,而不是从顶部开始的第一个空白行。 Any attempt to manipulate the destination has been unsuccessful.任何操纵目的地的尝试都没有成功。 I was able to have it paste at the top but it kept overwriting the same cell.我能够将它粘贴在顶部,但它一直覆盖同一个单元格。
function copyData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sName = ss.getSheetName();
if (sName == 'Activity Report'){
var source = ss.getRange("Activity Report!J3");
var destSheet = ss.getSheetByName("Activity Report");
var lastRow = destSheet.getLastRow();
var destination = destSheet.getRange("L"+lastRow);
destination.activate();
source.copyTo(destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
}
Thanks in advance!提前致谢!
Copying data to first empty cell in Columnn L将数据复制到 Column L 中的第一个空单元格
Copy value from J3
to bottom of column L将值从J3
复制到 L 列底部
function copyData() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Activity Report');
sh.getRange(sh.getLastRow()+1,12).setValue(sh.getRange(3,10).getValue());
}
Try this:试试这个:
This will put it into the very first empty cell in column L which could be the lastRow()+1 if there are no blanks.这会将其放入 L 列的第一个空单元格中,如果没有空格,则该单元格可能是 lastRow()+1 。
function runOne() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Activity Report');
var f=false;
var L=sh.getRange(1,12,sh.getLastRow()+1,1).getValues().map(function(r,i){if(!f && r[0].toString().length==0){f=true;return i+1;}else{return '';}}).filter(function(e){return e;});
sh.getRange(L,12).setValue(sh.getRange(3,10).getValue());
}
This uses reduce:这使用减少:
If nothing else it's a bit simpler.如果不出意外,它会更简单一些。
function copyData() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Activity Report');
var f=false;
var L=sh.getRange(1,12,sh.getLastRow()+1,1).getValues().reduce(function(a,r,i){if(!f && r[0].toString().length==0){f=true;return a + i;}return a;},1);
sh.getRange(L,12).setValue(sh.getRange(3,10).getValue());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.