Google Apps Script Copying a column in one sheet, transposing it, then pasting to another

287 views
Skip to first unread message

Picloan Limited

unread,
Jun 16, 2021, 12:18:53 PM6/16/21
to Google Apps Script Community
I have a sheet that is set up in the template of a form, with data prompts like Name, ID#, etc. in column A and the actual data that is inputted in column B. I have created a button labeled 'Submit Form' that is linked to the script. What I want this script to achieve is to copy only the data from a specific range in column B, then paste that data into the next empty row in a new sheet to create a sort of database of the form responses. Please help me


function runOne() { 
  var ss = SpreadsheetApp.getActive(); 
  var sheet1 = ss.getSheetByName("RMA"); 
  var sheet2 = ss.getSheetByName('RMA Database'); 
  var vA=sheet1.getRange("B5:B25").getValues().map(function(r){return r[0]}); sheet2.appendRow(vA); 
 sheet1.getRange('B5:B25').clearContent(); }

Michael O'Shaughnessy

unread,
Jun 16, 2021, 6:42:27 PM6/16/21
to google-apps-sc...@googlegroups.com
I am not sure where your error is coming from, however I have a feeling it has to do with trying to append a 2d array to a single row.  

Take a look at this quick code (I only used 3 rows) and read the comments.  I tried to describe what is being done:
function trans() {
let ss = SpreadsheetApp.getActiveSpreadsheet();
let shtIn = ss.getSheetByName("in");
let shtOut = ss.getSheetByName("out");

let responses = shtIn.getRange("B2:B4").getValues();
//responses is a 2d array that looks like this [[a],[b],[c]]
//first transpose it
let transResp = responses[0].map((resp,i)=>responses.map(r => r[i]))
//we now have [[a,b,c]]
//BUT this is still 2d array. the appendRow function needs a 1d array
//So... you can do one of two things...
//1. just use the first row
shtOut.appendRow(transResp[0])
//2. you can use array.flat()
shtOut.appendRow(transResp.flat())

//Clear the 'form'
shtIn.getRange("B2:B4").clearContent();
}



--
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 on the web visit https://groups.google.com/d/msgid/google-apps-script-community/febd2148-f1bf-47db-bb74-4a495bd9b86cn%40googlegroups.com.

Picloan Limited

unread,
Jun 17, 2021, 1:11:56 PM6/17/21
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages