简体   繁体   中英

Set column default value in Google Sheets

I have a finance tracking worksheet that has a number of formulas for each row. What I'd like to add is have these formulas replicate whenever a new row is inserted (fill it with a "default value").

Sheet before adding the row:

在添加行之前

Sheet after adding the row:

添加行后

I'd like some of these cells (B28 in this case) to contain a formula. for this row the formula is:

=IF(ISBLANK(C27), "", CONCATENATE(YEAR(C27), "-", MONTH(C27)))

You might want to consider an ArrayFormula. Here's a place for getting detail. In your case, the big trick is knowing that you can run the formula through all of Column C using the notation something like C1:C. Doing it this way is far more self documenting than burying it in a Google function and it's way more general.

Because the formula is the same for all rows, I added a function that just does the following:

function addRow() {
  SpreadsheetApp.getActiveSheet().getRange('B7:B').setValue('=IF(ISBLANK(C7), "", CONCATENATE(YEAR(C7), "-", MONTH(C7)))');
}

I set this function to run on a trigger whenever the sheet is edited. The row numbers increment themselves so not problem there either.

Used this SO question for help: Default cell values when Inserting new row in Google Sheets

删除B列中的所有内容并将其粘贴到B1单元格中:

={"Date", ARRAYFORMULA(IF(C2:C<>"", YEAR(C2:C)&"-"&MONTH(C2:C)))}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM