Coping automatically the value of a cell adding some rows

431 views
Skip to first unread message

Balubeto Balubeto

unread,
Oct 24, 2025, 5:51:47 AMOct 24
to Google Apps Script Community
Hi

I would like that every time I add one or more rows in areas where column A contains the cell with the “Date e ricevute” string, the added rows automatically have the aforementioned string in their column A cells.

I have created this sample spreadsheet:

https://docs.google.com/spreadsheets/d/1o7Jq2419Vglp6gmBccK17gwt3gq2LsKEpYZT7m3HVQ0/edit?usp=drive_link

Thanks

Bye

Brent Guttmann

unread,
Oct 24, 2025, 9:49:25 PMOct 24
to Google Apps Script Community
const C_GRUPO = '#e0f2f1', C_SUB = '#f5f5f5', C_TOTAL = '#ffffff',
      C_DATA = '#fff9c4', C_PERS = '#ffe0b2',
      TXT_MAG = '#ff00ff', TXT_GRAY = '#999999', TXT_BLK = '#000000';

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('🎨 Formattazione colori categorie spesa')
    .addItem('Applica colori categorie', 'applyFmt')
    .addToUi();
}

function onEdit(e) {
  const s = e.range.getSheet(), r = e.range;
  if (r.getColumn() !== 1) return;

  const row = r.getRow(),
        val = r.getValue(),
        above = row > 1 ? s.getRange(row - 1, 1).getValue() : '';

  if (!val && above === 'Date e ricevute') {
    s.getRange(row, 1).setValue('Date e ricevute');
  }

  if (s.getLastRow() >= 7) applyFmt();
}

function onChange(e) {
  if (e.changeType !== 'INSERT_ROW') return;
  const sh = e.source.getActiveSheet();
  const rows = sh.getActiveRangeList()?.getRanges()?.[0];
  if (!rows) return;
  const start = rows.getRow();
  const count = rows.getNumRows();
  const above = start > 1 ? sh.getRange(start - 1, 1).getValue() : '';
  if (above === 'Date e ricevute') {
    const vals = Array.from({length: count}, () => ['Date e ricevute']);
    sh.getRange(start, 1, count, 1).setValues(vals);
  }
  applyFmt();
}

function applyFmt() {
  const sh = SpreadsheetApp.getActiveSheet();
  const last = sh.getLastRow();
  if (last < 7) return;

  const rng = sh.getRange('A7:Z' + last),
        fc = rng.getFontColors(),
        nf = rng.getNumberFormats(),
        bg = rng.getBackgrounds();

  for (let i = 0; i < fc.length; i++) {
    let clr = null;
    switch (fc[i][0]) {
      case TXT_MAG: clr = C_GRUPO; break;
      case TXT_GRAY: clr = C_SUB; break;
      case TXT_BLK:
        if (rng.getCell(i + 1, 1).getValue()) clr = C_TOTAL;
        break;
    }
    if (clr) bg[i].fill(clr);
  }

  for (let i = 0; i < nf.length; i++) {
    for (let j = 0; j < nf[i].length; j++) {
      const f = nf[i][j];
      if (/[dmy]/.test(f)) bg[i][j] = C_DATA;
      else if (f === '# ???') bg[i][j] = C_PERS;
    }
  }

  rng.setBackgrounds(bg);
}

You will need to add an onChange trigger in the triggers page of the sheet's scripts page.
  • Function to run: onChange
  • Source: Spreadsheet
  • Type: On Change

Balubeto Balubeto

unread,
Oct 25, 2025, 4:30:38 AMOct 25
to Google Apps Script Community
I tried it, but it doesn't work because I inserted the rows 10, 11, and 14 as example, but the “Date e ricevute” string  does not appear in cells A10, A11, and A14.

In addition, the color of cells B10:Y10, B11:Y11, and B14:Y14 are not the same color as the rows above them.

How come?

Thanks

Bye

Brent Guttmann

unread,
Oct 25, 2025, 8:44:28 AMOct 25
to Google Apps Script Community
Did you set up the trigger?

Balubeto Balubeto

unread,
Oct 26, 2025, 4:42:27 AMOct 26
to Google Apps Script Community
I made the change but nothing has changed, i.e., the cells in column A of the inserted rows remain empty.

Why is that?

Thanks

Bye

Brent Guttmann

unread,
Oct 26, 2025, 11:09:43 AMOct 26
to google-apps-sc...@googlegroups.com

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/J2PHg-Ha7p8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/a17327ca-cb35-4ac4-af0e-ac6a0785b0fan%40googlegroups.com.
Message has been deleted

Balubeto Balubeto

unread,
Oct 27, 2025, 5:39:20 AMOct 27
to Google Apps Script Community

Now the script works, and I've learned something new thanks to you.

To conclude, in the rows where the cells in column A contain the "Date e ricevute” string is it possible to ensure that the related Date cells (i.e., those in columns B, D, F, H, J, L, N, P, R, T, V, and X) are yellow with blue text and those of the Custom Currency type (i.e., those in columns C, E, G, I, K, M, O, Q, S, U, W, and Y) are blue with yellow text?

Thanks

Bye

Balubeto Balubeto

unread,
Oct 30, 2025, 12:23:50 PMOct 30
to Google Apps Script Community
Could you finish your script, as I really need it (see above)?

Thanks

Bye

Brent Guttmann

unread,
Oct 30, 2025, 12:33:59 PMOct 30
to google-apps-sc...@googlegroups.com
Sorry, I do not have time for this. I suggest you use conditional formatting. I am sure chatgpt can help you out. Good luck!

On Thu, Oct 30, 2025 at 12:24 PM Balubeto Balubeto <balu...@gmail.com> wrote:
Could you finish your script, as I really need it (see above)?

Thanks

Bye

Il giorno lunedì 27 ottobre 2025 alle 10:39:20 UTC+1 Balubeto Balubeto ha scritto:

Now the script works, and I've learned something new thanks to you.

To conclude, in the rows where the cells in column A contain the "Date e ricevute” string is it possible to ensure that the related Date cells (i.e., those in columns B, D, F, H, J, L, N, P, R, T, V, and X) are yellow with blue text and those of the Custom Currency type (i.e., those in columns C, E, G, I, K, M, O, Q, S, U, W, and Y) are blue with yellow text?

Thanks

Bye


Il giorno domenica 26 ottobre 2025 alle 16:09:43 UTC+1 Brent Guttmann ha scritto:

Balubeto Balubeto

unread,
Oct 31, 2025, 4:09:24 AMOct 31
to Google Apps Script Community
I understand.

When you have some time, remember to finish this script, as I think it will be very useful in several projects.

Thanks

Bye

Balubeto Balubeto

unread,
Oct 31, 2025, 7:31:00 AMOct 31
to Google Apps Script Community
Sorry again, but I noticed a bug in your script:

When I add a row, the blank rows 42, 47, and 54 are automatically colored.  How come?

Thanks

Bye  

Brent Guttmann

unread,
Oct 31, 2025, 9:12:59 AMOct 31
to google-apps-sc...@googlegroups.com

What I added only applies the same coloring to the row above the one that was inserted. I integrated it with the code you originally had, so you might want to check that section or your conditional formatting to identify the cause.

I recommend taking some time to review the code, understand how it works, and debug it yourself, as I don’t plan to spend further time on this. It might be a language barrier issue, but I also suggest being mindful of how you ask for help on this forum; at times, your requests can come across as if others owe you a solution.


On Fri, Oct 31, 2025 at 7:31 AM Balubeto Balubeto <balu...@gmail.com> wrote:
Sorry again, but I noticed a bug in your script:

When I add a row, the blank rows 42, 47, and 54 are automatically colored.  How come?

Thanks

Bye  

Il giorno venerdì 31 ottobre 2025 alle 09:09:24 UTC+1 Balubeto Balubeto ha scritto:
I understand.

When you have some time, remember to finish this script, as I think it will be very useful in several projects.

Thanks

Bye

Il giorno giovedì 30 ottobre 2025 alle 17:33:59 UTC+1 Brent Guttmann ha scritto:
Sorry, I do not have time for this. I suggest you use conditional formatting. I am sure chatgpt can help you out. Good luck!

Balubeto Balubeto

unread,
Nov 1, 2025, 4:26:22 AMNov 1
to Google Apps Script Community
What a nice problem, since I don't know anything about programming.

Please, could you fix that bug?

Thanks

Bye

Balubeto Balubeto

unread,
Nov 3, 2025, 11:44:44 AMNov 3
to Google Apps Script Community
This morning, I opened my spreadsheet and neither the Extensions menu nor your script menu were there.
Now, only the first menu has reappeared.

What happened?

Thanks

Bye

Brent Guttmann

unread,
Nov 3, 2025, 12:59:00 PMNov 3
to google-apps-sc...@googlegroups.com
I completed and tested your code fix on October 30th, despite saying I didn’t have time. The change is noted in the comment at the top of the script. You’re welcome. I’ve spent all the time I intend to on this and won’t be making additional updates.

Take care,

Brent

On Mon, Nov 3, 2025 at 11:45 AM Balubeto Balubeto <balu...@gmail.com> wrote:
This morning, I opened my spreadsheet and neither the Extensions menu nor your script menu were there.
Now, only the first menu has reappeared.

What happened?

Thanks

Bye

Il giorno sabato 1 novembre 2025 alle 09:26:22 UTC+1 Balubeto Balubeto ha scritto:
What a nice problem, since I don't know anything about programming.

Please, could you fix that bug?

Thanks

Bye

Il giorno venerdì 31 ottobre 2025 alle 14:12:59 UTC+1 Brent Guttmann ha scritto:

What I added only applies the same coloring to the row above the one that was inserted. I integrated it with the code you originally had, so you might want to check that section or your conditional formatting to identify the cause.

I recommend taking some time to review the code, understand how it works, and debug it yourself, as I don’t plan to spend further time on this. It might be a language barrier issue, but I also suggest being mindful of how you ask for help on this forum; at times, your requests can come across as if others owe you a solution.


Balubeto Balubeto

unread,
Nov 4, 2025, 12:43:30 PMNov 4
to Google Apps Script Community

Finally, I have restored my spreadsheet with your script working.

The thing I don't understand is why the cells of rows 70 and 71 and those of D131, D132, D133, and D134 are colored in yellow, since your script should ignore the aforementioned cells.


Thanks

Bye

Keith Andersen

unread,
Nov 4, 2025, 12:54:31 PMNov 4
to google-apps-sc...@googlegroups.com
Understandably, Brent has limited time to spend on this project. If you'll share It with me. I will try and help work out some of the bugs you're finding.

Keith 



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/575ceeb0-b129-4bd3-938d-016a866979f6n%40googlegroups.com.

Balubeto Balubeto

unread,
Nov 4, 2025, 1:26:51 PMNov 4
to Google Apps Script Community

The link to my spreadsheet is in the first message.

Thanks

Bye

Keith Andersen

unread,
Nov 4, 2025, 2:13:08 PMNov 4
to google-apps-sc...@googlegroups.com
Could you redo it because it's not working for me. Please repost here.



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

Balubeto Balubeto

unread,
Nov 5, 2025, 3:01:10 AMNov 5
to Google Apps Script Community

Balubeto Balubeto

unread,
Nov 6, 2025, 12:05:43 PMNov 6
to Google Apps Script Community
Now, when I run the script, I get the following errors:

Esempio_01_-_Apps_Script.png
Esempio_01_-_Sheet.png
How come?

Thanks

Bye

Balubeto Balubeto

unread,
Nov 8, 2025, 6:30:52 AMNov 8
to Google Apps Script Community
How come I get the above error?
How can I fix it?

Thanks

Bye

Balubeto Balubeto

unread,
Nov 11, 2025, 4:48:26 AMNov 11
to Google Apps Script Community
I noticed that when I run the script from the menu, the execution function is ApplyFmt rather than OnChange, and I get the error “ReferenceError: v is not defined.” How come?

Thanks

Bye

Keith Andersen

unread,
Nov 11, 2025, 9:53:57 AMNov 11
to google-apps-sc...@googlegroups.com
At this point - please explain exactly what you want/need the script to do. Please be as specific as possible.



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

Balubeto Balubeto

unread,
Nov 11, 2025, 12:15:59 PMNov 11
to Google Apps Script Community
When I run the script from Apps Script, I get the error " TypeError: Cannot read properties of undefined (reading ‘changeType’)":

Esempio_01_-_Apps_Script.png

When I run it from the spreadsheet menu, I get the error “ReferenceError: v is not defined”:

Esempio_01_-_Sheet.png

Why are these errors displayed?

How can I fix these errors so that this script no longer displays them?

Thanks

Bye

Keith Andersen

unread,
Nov 11, 2025, 12:18:50 PMNov 11
to google-apps-sc...@googlegroups.com
Again...
At this point - please explain exactly what you want/need the script to do. Please be as specific as possible about your process needs / expectations. Knowing this then I can look at the script to see what it's supposed to do.



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

Brent Guttmann

unread,
Nov 11, 2025, 12:39:24 PMNov 11
to google-apps-sc...@googlegroups.com
You cannot run onChange from the appscript editor, it expects an event (e) to be passed -- onChange(e) 

--

Balubeto Balubeto

unread,
Nov 11, 2025, 1:14:37 PMNov 11
to Google Apps Script Community
So, in Apps Script, what function should I set instead of OnChange?

Thanks

Bye

Brent Guttmann

unread,
Nov 11, 2025, 2:11:29 PMNov 11
to google-apps-sc...@googlegroups.com
I don't understand what you are asking. The onChange event is handled in the triggers, as I explained previously. This was working as intended. If you edit the code or make changes to the sheet structure/format yourself, you should be prepared to resolve any errors created by those changes.

Keith has graciously offered to help, but you need to answer the following questions for him to assist you:
  • What was the goal of the action you were performing when the error occurred?
  • What did you expect to happen?
  • What was the error exactly?

On Tue, Nov 11, 2025 at 1:14 PM Balubeto Balubeto <balu...@gmail.com> wrote:
So, in Apps Script, what function should I set instead of OnChange?

Thanks

Bye

Il giorno martedì 11 novembre 2025 alle 18:39:24 UTC+1 Brent Guttmann ha scritto:
You cannot run onChange from the appscript editor, it expects an event (e) to be passed -- onChange(e) 

Balubeto Balubeto

unread,
Nov 12, 2025, 1:00:00 PMNov 12
to Google Apps Script Community
When I run the script, the error rate is 25.53%. How come?

When I run it in Apps Script with the onOpen function set, I get the error “Exception: Cannot call SpreadsheetApp.getUi() from this context.” How come?

However, when I run it from the spreadsheet menu, the script seems to run correctly, but at the end of its execution, the error “ReferenceError: v is not defined” always appears. Why?

So, what should I do to have an error-free script?

Thanks

Bye

Brent Guttmann

unread,
Nov 12, 2025, 2:00:36 PMNov 12
to google-apps-sc...@googlegroups.com
I dont understand what you are running.

When I left it, it ran by itself automatically when you inserted a row. That is the entire purpose of the onChange event. There was no benefit or reason to run this script manually.


Message has been deleted

Balubeto Balubeto

unread,
Nov 13, 2025, 3:30:23 AMNov 13
to Google Apps Script Community

Try running the entire script from the spreadsheet menu "Formattazione colori categorie spesa" and you will see the error message “ReferenceError: v is not defined.”

Thanks

Bye

Keith Andersen

unread,
Nov 13, 2025, 3:36:25 AMNov 13
to google-apps-sc...@googlegroups.com
What exactly, and please make the effort to explain and be specific, is your purpose for the script?  Please tell me like we're starting at square one ...what it is that you need the script to do on your spreadsheet?  It is only then that I, or anyone else, can formulate a process to achieve your goal. It is too far for us to go back and look through several different emails and the numerous emails in this thread to try and figure it out on our own.... We need you to explain it. 

Once you explain it fully and we get an understanding of what the script is supposed to do, we can formulate scripts to accomplish that goal through a tested process. Brent has already done that... but we need to understand the process.

Does that make sense to you? Do you have any questions?



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

Balubeto Balubeto

unread,
Nov 13, 2025, 4:11:53 AMNov 13
to Google Apps Script Community
The script works, but when I run it from the spreadsheet menu "Formattazione colori categorie spesa", the error message “ReferenceError: v is not defined” is displayed.

Why is this happening? How can I fix the problem?

Keith Andersen

unread,
Nov 13, 2025, 1:01:11 PMNov 13
to google-apps-sc...@googlegroups.com
It appears that per your original request - you wanted format changes to be applied AUTOMATICALLY when certain changes were made to the spreadsheet.

Brent's script solution was to incorporate onEdit / onChange functions to AUTOMATICALLY make those changes AS YOU REQUESTED.

Therefore - there is no need or reason to run the "Formattazione colori categorie spesa" from the Menu. Your solution (from Brent) is not designed to be run manually - through the menu.  DON'T RUN "Formattazione colori categorie spesa"!

Keith




--

Passions: God, Family, Friends, Scripture, Data Management, Google Sheets + App Script, MS Access, Programing, sharing and much more.

Balubeto Balubeto

unread,
Nov 13, 2025, 1:22:20 PMNov 13
to Google Apps Script Community
So, can I ignore the error messages mentioned above?

Thanks

Bye

Keith Andersen

unread,
Nov 13, 2025, 1:25:31 PMNov 13
to google-apps-sc...@googlegroups.com
DON'T RUN IT MANUALLY FROM THE MENU. Then you won't get the error messages. In fact- delete the custom menu or de-activate it.



Balubeto Balubeto

unread,
Nov 14, 2025, 3:54:21 AMNov 14
to Google Apps Script Community
A curiosity: If I insert this script into a new spreadsheet with the same structure, how can I apply all the features of this script?

Thanks

Bye

Balubeto Balubeto

unread,
Nov 14, 2025, 1:42:32 PMNov 14
to Google Apps Script Community
I noticed something:
The constant C_TOTAL = ‘#ffffff’ does not work because the cells of the rows 47, 107, and 129 are not all white but yellow. How come?

Thanks

Bye

Balubeto Balubeto

unread,
Nov 16, 2025, 4:17:55 AMNov 16
to Google Apps Script Community
In other words, I would like the color of the cells in the rows containing the words “Totale mensile” in column A should be white, as it was a month ago.

Thanks

Bye

Balubeto Balubeto

unread,
Nov 17, 2025, 12:57:44 PMNov 17
to Google Apps Script Community
How could I solve the problems mentioned above?

Thanks

Bye

Balubeto Balubeto

unread,
Nov 19, 2025, 10:55:29 AMNov 19
to Google Apps Script Community
When you have time, can you still help me?

Thanks

Bye

Keith Andersen

unread,
Nov 19, 2025, 11:01:30 AMNov 19
to google-apps-sc...@googlegroups.com
Did those rows change color 1 time? Do they still charge colors?

Why not manually reset this rows that appeared to have randomly changed... And then look for the process that might change them or does change them randomly. 

Right now I have no idea why those particular few rows changed color.



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

Balubeto Balubeto

unread,
Nov 19, 2025, 11:07:28 AMNov 19
to Google Apps Script Community
 I would like the color of the cells in the rows containing the words “Totale mensile” in column A should be white.

Thanks

Bye

Brent Guttmann

unread,
Nov 19, 2025, 11:17:42 AMNov 19
to google-apps-sc...@googlegroups.com
I fixed this.

Balubeto Balubeto

unread,
Nov 19, 2025, 11:20:31 AMNov 19
to Google Apps Script Community
 I would like the color of the cells in the rows containing the words “Totale mensile” in column A should be white to differentiate them from the others.

Thanks

Bye

Balubeto Balubeto

unread,
Nov 19, 2025, 11:30:17 AMNov 19
to Google Apps Script Community

Thank you very much


Bye

Balubeto Balubeto

unread,
Nov 20, 2025, 6:22:58 AM (14 days ago) Nov 20
to Google Apps Script Community
I noticed that, after Brent Guttmann's latest modification, I can also run the script from the spreadsheet menu without getting any errors.

Now, since I should create other similar projects, I would need the rows where the cells containing the words “Date e ricevute” in column A have the cells in columns B, D, F, H, J, L, N, P, R, T, V, and X colored in yellow with the blue writing, and those in columns C, E, G, I, K, M, O, Q, S, U, W, and Y colored in blue with the yellow writing.

Is it possible to do this change?

Thanks

Bye

Brent Guttmann

unread,
Nov 20, 2025, 6:48:52 AM (14 days ago) Nov 20
to google-apps-sc...@googlegroups.com
Sorry, but I am done helping on this.

Balubeto Balubeto

unread,
Nov 25, 2025, 12:29:59 PM (9 days ago) Nov 25
to Google Apps Script Community
I noticed that the script does not automatically color the cells from column B to column Y when I add a new expense group (see the "Spese mediche” group).

How come?

Thanks

Bye

Balubeto Balubeto

unread,
Nov 30, 2025, 4:05:27 AM (4 days ago) Nov 30
to Google Apps Script Community

Could you please help me again (see above)?

Thanks

Bye

Reply all
Reply to author
Forward
0 new messages