在Google表格中插入新行时的默认单元格值 [英] Default cell values when Inserting new row in Google Sheets
问题描述
在Google表格中,我希望在上面/下面插入1行,并在此新行中的某些单元格具有默认值。 例如,当添加一个新行时,此行C列中的单元格将具有字母N,列G将具有Y,并且列T将具有= sum(A2 + B2)的公式 - 当然,A2和B2需要反映这一新行的数量。
到目前为止:
function addRow(){
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell();
var range = source.getRange('SAMPLE ROW'!A2:AJ2);
range.copyValuesToRange(0,4,6,4,6);
}
source.getRange是从我正在使用的隐藏选项卡获取数据。
如何在copyValuestoRange部分使用当前行?
要插入新行,您可以使用函数 insertRowBefore 或 insertRowAfter ,您只需要指示将插入新行的引用索引(整数)。
sheet.insertRowBefore(2); //在第二行之前插入一个新行。
要将值插入到新行中的单元格中,可以使用范围中的函数setValue()。
sheet.getRange(2,3,1,1).setValue('N'); //这将在列C中插入值N.
您也可以使用a1Notaion来选择范围。例如getRange( C2’ )。以下是有关 getRange 的文档以查看详情。
我不明白你的问题如何使用copyValuestoRange中的当前行?。
要从一个范围中获取值,您必须调用范围函数getValues()。它将返回一个二维数组。然后使用范围函数setValues(),您可以使用该数组将这些值插入到新范围中。
var values = source .getRange('SAMPLE ROW'!A2:AJ2)。getValues();
sheet.getRange('range_you_need')。setValues(values);
希望这有助于您。
In Google Sheets, I would like to "Insert 1 row above/below" and have certain cells in this new row to have default values.
For example, when a new row is added, the cell in column C for this row will have the letter "N", column G will have a "Y", and column T will have a formula of "=sum(A2+B2)" - of course the A2 and B2 will need to reflect the number of this new row.
What I have so far:
function addRow() {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell();
var range = source.getRange("'SAMPLE ROW'!A2:AJ2");
range.copyValuesToRange(0, 4, 6, 4, 6);
}
The source.getRange is grabbing data from a hidden tab I am using.
How do I use the current Row in the copyValuestoRange section?
To insert a new row you can use the function insertRowBefore or insertRowAfter, you just have to indicate the index (integer) of reference where the new row will be inserted.
sheet.insertRowBefore(2); //Here a new row will be inserted before the second row.
To insert values to the cells in the new row you can use the function setValue() from range.
sheet.getRange(2,3,1,1).setValue('N'); // this will insert the value N in the column C.
You can also use the a1Notaion to select a range. e.g. getRange('C2'). Here is the documentation about getRange check it for more details.
I didn't understand your question "How do I use the current Row in the copyValuestoRange section?".
to get the values from a range, you have to call the range function getValues(). It will return a two dimensional array. then with the range function setValues() you can use that array to insert those values in the new range.
var values = source.getRange("'SAMPLE ROW'!A2:AJ2").getValues();
sheet.getRange('range_you_need').setValues(values);
Hope this helps.
这篇关于在Google表格中插入新行时的默认单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!