[英]Copy/Pasting a Range of Values into the Next Empty Set of Cells on a Separate Sheet
I am attempting to take data from a "worksheet" which will always begin as the active sheet into a seperate sheet that is determined by the month of the year.我试图从始终作为活动工作表开始的“工作表”中获取数据,并将其放入由一年中的月份确定的单独工作表中。 That has been no problem so far and my code does function in this way.
到目前为止这没有问题,我的代码确实以这种方式运行。 However I can not understand how to take that predetermined amount of info "A9:AG11" in our static worksheet and paste into the next empty set of rows on our shared monthly sheet.
但是我不明白如何在我们的静态工作表中获取预定数量的信息“A9:AG11”并粘贴到我们共享的月度表上的下一组空行中。 Here is my current code that does work, ui button, and all.
这是我当前有效的代码、ui 按钮和所有代码。 But it will of course only write over any data already existing on the second sheet.
但它当然只会覆盖第二张纸上已经存在的任何数据。 Any help would be appreciated!
任何帮助,将不胜感激!
function BP3CallTab() {
var ui = SpreadsheetApp.getUi(); // Same variations.
var result = ui.alert(
'Please confirm',
'Ready to Submit 3-Call Audit?',
ui.ButtonSet.YES_NO);
// Process the user's response.
if (result == ui.Button.YES) {
// User clicked "Yes".
copyFunction ();
}
function copyFunction () {
var inputRange = SpreadsheetApp.getActiveSpreadsheet().getRange("A9:AG11");
var inputValues = inputRange.getValues();
var shname = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dash").getRange("Q4").getValue()
var emptyrow = SpreadsheetApp.getActive();
emptyrow.getRange('A17').activate();
emptyrow.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
emptyrow.getCurrentCell().offset(1, 0).activate();
var outputRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getRange("A17:AG19").setValues(inputValues);
}
ui.alert('Audit Complete!✅');
if (result ==ui.Button.NO) {
// User clicked "No" or X in the title bar.
ui.alert('No Changes Made.');
}
};
You need to use a different method to define the output range.您需要使用不同的方法来定义输出范围。
Try getRange(row, column, numRows, numColumns) .试试getRange(row, column, numRows, numColumns) 。
"row" is obtained by using getLastRow()
, say... “行”是通过使用
getLastRow()
获得的,比如说......
var targetLR = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getLastRow()
"Column" =1 (to denote column A) "Column" =1(表示 A 列)
"numRows" = 3 (the number of rows between "A17:A19") “numRows”= 3(“A17:A19”之间的行数)
"numColumns" = 33 (the number of columns between "A" and "AG") “numColumns”= 33(“A”和“AG”之间的列数)
So your output range would be something like:所以你的输出范围是这样的:
var outputRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getRange(targetLR,1,2,33).setValues(inputValues)
A slight variation of @Tedinoz's approach so that (a) the destination range is always exactly the same as the size of data being copied, otherwise it results to an error and (b) the checking of the next empty row is still correct even if a cell somewhere down below was accidentally edited --- something that affects getLastRow() @Tedinoz 的方法略有不同,因此 (a) 目标范围始终与被复制数据的大小完全相同,否则会导致错误,并且 (b) 下一个空行的检查仍然正确,即使下面某处的单元格被意外编辑了——影响 getLastRow() 的东西
function copyFunction () {
var inputRange = SpreadsheetApp.getActiveSpreadsheet().getRange("A9:AG11");
var inputValues = inputRange.getValues();
var inputValuesRows = inputValues.length; // no. of rows of copied data
var inputValuesCols = inputValues[0].length; // no. of columns of copied data
//get your destination sheet and its first empty row
var sharedSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Shared Monthly Sheet");
var firstEmptyRow = getFirstEmptyWholeRow("Shared Monthly Sheet");
Logger.log(firstEmptyRow);
//copy your data into your destination starting from firstEmptyRow and column A
sharedSheet.getRange(firstEmptyRow,1,inputValuesRows,inputValuesCols).setValues(inputValues);
}
/*
* Adapted from Mogsdad's "whole row" checker
* from https://stackoverflow.com/questions/6882104/faster-way-to-find-the-first-empty-row-in-a-google-sheet-column
*/
function getFirstEmptyWholeRow(sheetname) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var range = sheet.getDataRange();
var values = range.getValues();
var row = 0;
for (var row=0; row<values.length; row++) {
if (!values[row].join("")) break;
}
return (row+1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.