![](/img/trans.png)
[英]Copy and Paste Values of Cells to another Spreadsheet, creating a new sheet and pasting values
[英]Copy/Pasting a Range of Values into the Next Empty Set of Cells on a Separate Sheet
我试图从始终作为活动工作表开始的“工作表”中获取数据,并将其放入由一年中的月份确定的单独工作表中。 到目前为止这没有问题,我的代码确实以这种方式运行。 但是我不明白如何在我们的静态工作表中获取预定数量的信息“A9:AG11”并粘贴到我们共享的月度表上的下一组空行中。 这是我当前有效的代码、ui 按钮和所有代码。 但它当然只会覆盖第二张纸上已经存在的任何数据。 任何帮助,将不胜感激!
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.');
}
};
您需要使用不同的方法来定义输出范围。
“行”是通过使用getLastRow()
获得的,比如说......
var targetLR = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getLastRow()
"Column" =1(表示 A 列)
“numRows”= 3(“A17:A19”之间的行数)
“numColumns”= 33(“A”和“AG”之间的列数)
所以你的输出范围是这样的:
var outputRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getRange(targetLR,1,2,33).setValues(inputValues)
@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.