r/GoogleAppsScript • • Jun 01 '25

Resolved For google sheets: Is it possible to write a script to auto-copy only some columns of a new row of data from one sheet to another while keeping (in the receiving sheet) active columns that contain formulas?

The Set up (all within same worksheet):

Sheet 1: receives google form submissions data that populates columns A, B, C, D

Sheet 2: is set up with formulas in columns E and F to process data from A, B, C, D. Column G is a manual entry column that E and F may also pull from, conditionally.

Desired work flow: When a new form submission happens, I want only data from columns A, B, C and D of sheet 1 to come over to sheet 2 and integrate with the columns/ formulas that are already there (as opposed to also all the additional empty columns to the right in sheet one (E, F, G) over-writing the formulas that were "in waiting" in E and F of sheet 2.

4 Upvotes

20 comments sorted by

4

u/THE_Aft_io9_Giz Jun 01 '25

yes, that's how I have most of my sheets setup. You can even take it further and have the columns sorted in the new worksheet, if you desire. I do this for an auction form so it doesn't matter when the bid comes in, the highest bid will show up at the top of the list so we can show a live view of the embedded worksheet in a google site. You can also build dynamic graphs and tables from this setup so as people fill out the forms only certain data comes into the worksheets that are setup for whatever specific output you are wanting. I used chatgpt for everything in google apps scripts. I tell it what I want, just like your message and then it outputs a script and then I have it walk me through exactly how to setup the script. There is definitely some trial and error with chatgpt and the scripts, but I've never not been able to get the script I want.

Here is the chatgpt output to try:

To achieve your desired workflow — where only columns A, B, C, and D from Sheet 1 are copied to Sheet 2, without overwriting formulas in columns E, F, or G of Sheet 2 — here’s a step-by-step Google Apps Script you can use:

✅ How It Works

  • Every time a new form submission is made, the script copies only columns A–D from Sheet 1 to Sheet 2.
  • It pastes the values in columns A–D of Sheet 2 starting from row 2, leaving your formulas in E–G intact.

✅ Google Apps Script

  1. Open your Sheet.
  2. Click Extensions > Apps Script.
  3. Delete any default code and paste the following:
function copyFormDataToSheet2() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet1 = ss.getSheetByName("Sheet1"); // replace with your form response sheet name
  const sheet2 = ss.getSheetByName("Sheet2"); // replace with your processing sheet name

  const lastRow = sheet1.getLastRow();
  const data = sheet1.getRange(2, 1, lastRow - 1, 4).getValues(); // A-D data only

  // Clear existing A-D data in Sheet2 (but not E-G!)
  sheet2.getRange(2, 1, sheet2.getLastRow(), 4).clearContent();

  // Paste fresh A-D data into Sheet2
  sheet2.getRange(2, 1, data.length, 4).setValues(data);
}

2

u/THE_Aft_io9_Giz Jun 01 '25

✅ Optional: Trigger the Script on Form Submit

  1. In the Apps Script editor, click the clock icon ⏰ on the left sidebar (Triggers).
  2. Click "+ Add Trigger".
  3. Choose:

    • Function: copyFormDataToSheet2
    • Event: "From spreadsheet" → "On form submit"
  4. Click Save.

🛠️ Notes

  • Ensure your formulas in E and F on Sheet 2 are set up to auto-fill as new rows are added. Use ArrayFormula if needed.
  • If manual entry in G should persist even after new submissions, this script won’t affect column G since only A–D are cleared and re-pasted.

Would you like help setting up the formulas in E and F to auto-extend or use ArrayFormula too?

1

u/longunderscorestory Jun 01 '25

Thanks a lot for this

1

u/longunderscorestory Jun 01 '25

just what I needed

1

u/longunderscorestory Jun 01 '25

"Would you like help setting up the formulas in E and F to auto-extend or use ArrayFormula too?"

Is this chatGPT asking you a question or you're asking me? Excuse me for being a layman.

2

u/longunderscorestory Jun 01 '25

would love to see your auction worksheet. dm?

2

u/THE_Aft_io9_Giz Jun 01 '25

Asking me a question. It usually gives me what I want initially and then ask me a question also recall that it doesn't always get it right so sometimes depending on how your sheets are set up or the type of data in your Fields the initial formula doesn't work and you have to either copy paste upload a screenshot or just describe what happened and usually it'll come up with a few things to try and almost always solves it so it's not a perfect but it's pretty dang good considering I'm not writing any code.

1

u/longunderscorestory Jun 01 '25

Got it. Did not realize chatGPT can do google sheets. Pretty amazing. Thanks again for all the detail. Much appreciated!!

→ More replies (1)

1

u/longunderscorestory Jun 01 '25

What would be the difference going one way or the other for array vs auto-extend?

→ More replies (1)

3

u/THE_Aft_io9_Giz Jun 01 '25

You have to tell chatgpt the name of your sheets, the source column order and column names so it knows where to point the script

→ More replies (1)

2

u/Hinji Jun 01 '25

Do you need to have a 2nd sheet?

Can you not/do you not want to use sheet queries to pull the data from sheet 1 and show in sheet 2?

But yes, you can use getLastRow() and getlastColumn() to return the position of the last row/column that have values in them and use that to move the data over.

1

u/longunderscorestory Jun 01 '25

That is a good question. In previous worksheets I've built, I'm used to doing things in steps and having , for example, a sheet where things populate, then another sheet where the data is processed and another for view (for example). Those were sheets built without scripts so a different process I'm thinking.

In general, I am not trained but I'm trying to prepare my instructions and my plan for a scripts guy so trying to learn what is and isn't possible.

Thanks a lot

2

u/Hinji Jun 01 '25

I feel ya, I am also new to this and I have a "raw data" sheet(s) and then my user-facing sheets that look pretty etc.

1

u/longunderscorestory Jun 01 '25

Got it. "show" is not for you but for others to view.

1

u/longunderscorestory Jun 01 '25

Sorry, you mean i can process on sheet 1 instead of 2 but still show in sheet 2?

"to return the position of the last row/column that have values in them and use that to move the data over" what do you mean here? if you can explain for a dummy

2

u/Hinji Jun 01 '25

I guess there are multiple approaches here you could take.

  1. If you have no control over how the data in Sheet 1 is populated, then after the form is submitted, you could run a function that takes the data from the columns you need (if it's always the same columns, you don't need the methods I mentioned) and adds them to Sheet 2.
  2. If you do have the data available to you before the data is written to Sheet 1, simply have the function that is doing the work add it to both sheets.

Edit: And regarding my methods, they help identify the last column/row that has content in is which would then help you to know what range of data should be copied over to sheet 2.

1

u/longunderscorestory Jun 01 '25

thanks for the clarifications and information. much appreciated!