简体   繁体   中英

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

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. It will also clear the data from the range in column B on the original sheet.

I already have a way to clear the selected range on the original sheet, as well as a way to copy the selected range to the new sheet while automatically starting from the first empty row. I am having trouble transposing the data, however, since it pastes into a column like the original data, as opposed to pasting into a row.

function submitForm() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet1 = ss.getSheetByName("RMA");
    var sheet2 = ss.getSheetByName('RMA Database');
    var values = sheet2.getRange("A:A").getValues();
    var maxIndex = values.reduce(function(maxIndex, row, index) {
        return row[0] === "" ? maxIndex : index;
}, 0);
    sheet2.setActiveRange(sheet2.getRange(maxIndex + 2, 1))


sheet1.getRange("B5:B25").copyTo(sheet2.getRange(sheet2.getLastRow()+1,1), 
{contentsOnly:true});

    sheet1.getRange('B5:B25').clearContent();

}

Try this:

I don't see the point of this line var values = sheet2.getRange("A:A").getValues(); so I just omitted it

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();
}

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