下面的代码可以工作,但是我认为有一种更为优雅的方法。我想将“客户”表(源)上的选定行(D:R)的一部分复制到“测试”表(目标),再复制到该表中的第一空白行(C:Q)。
该代码可以正常工作,但看起来效果不佳/不优雅。
function rowSelected() {
var customer_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Customer_Info");
var invoice_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var customer_selection_row = customer_sheet.getActiveRange().getRow();
//Browser.msgBox(customer_selection_row);
var customer_selection_start = 'D' + customer_selection_row;
var customer_selection_end = 'R' + customer_selection_row;
var customer_selection_range = customer_selection_start + ':' + customer_selection_end;
var source_range = customer_sheet.getRange(customer_selection_range);
customer_sheet.setActiveRange(source_range);
//Browser.msgBox(source_range.getA1Notation());
var invoice_selection_row = getFirstEmptyRowWholeRow();
var invoice_selection_start = 'C' + invoice_selection_row;
var invoice_selection_end = 'Q' + invoice_selection_row;
var invoice_selection_range = invoice_selection_start + ':' + invoice_selection_end;
var destination_range = invoice_sheet.getRange(invoice_selection_range);
invoice_sheet.setActiveRange(destination_range);
//Browser.msgBox(destination_range.getA1Notation());
customer_sheet.setActiveRange(source_range).copyTo((destination_range), {contentsOnly:true});
}
我在想有一种方法可以减少代码。
答案 0 :(得分:4)
Customer_Info
的有效范围行中的“ D”列复制到“ R”,将{{1}检索到的行中的列“ C”复制到“ Q” getFirstEmptyRowWholeRow()
中的}。如果我的理解是正确的,那么该修改如何?
Test
一次被声明为`ss``。SpreadsheetApp.getActiveSpreadsheet()
代替了getRange(row, column, numRows, numColumns)
。getRange(a1Notation)
,设置起始单元格后,可以从中复制源范围。destination_range
时,脚本将运行。Customer_Info
function rowSelected() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var customer_sheet = ss.getActiveSheet();
if (customer_sheet.getSheetName() == "Customer_Info") {
var invoice_sheet = ss.getSheetByName("Test");
var activeRange = ss.getActiveRange();
var source_range = customer_sheet.getRange(activeRange.getRow(), 4, activeRange.getNumRows(), 15).activate();
var destination_range = invoice_sheet.getRange(getFirstEmptyRowWholeRow(), 3); // or invoice_sheet.getRange("C" + getFirstEmptyRowWholeRow())
source_range.copyTo(destination_range, {contentsOnly: true});
}
}
的脚本也用于上述修改后的脚本。getFirstEmptyRowWholeRow()
上选择“ A1:A3”的范围并运行该脚本,则将从单元格的单元格中复制单元格“ D1:R3”的值。列“ C”和Customer_Info
的行。如果我误解了您的问题,而这不是您想要的,我表示歉意。