Search code examples
google-apps-scriptgoogle-sheetskeyboard-shortcutscopy-paste

Copy and Paste Cell on Form Submission


On form submission on Google Sheets, I want to be able to copy and paste one specific cell in the new row. I want to do this in the same cell, but as value. (It's a formula dependent on a variable that changes and I want it to paste as a value once submitted for historical data purposes).

EDIT: Apologies for being vague. Javascript is not my forte, and I'm new to google scripts. I took some code from Nate Sargent in a Quora answer and edited it as follows to do what I was looking for...

function pasteValue(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('INVOICES')
var lastRow = sheet.getDataRange().getValues();

lastRow.forEach(function (row,index) {
    if (row[1] == "") {  
        lastRow.length = index;
    }
});

var newRange = sheet.getRange(lastRow.length,13);
newRange.copyTo(newRange, {contentsOnly: true})
sheet.setActiveRange(newRange);
}

Solution

  • for that purpose there is a handy shortcut: CTRL+SHIFT+V which does that. simply use a standard copy and paste it with this combo to paste only values without formulas. with script you would need to do something like:

    function moveValuesOnly() { var ss = SpreadsheetApp.getActiveSpreadsheet();
                                var source = ss.getRange('Sheet1!A1');
                                source.copyTo(ss.getRange('Sheet2!B1'), {contentsOnly: true}); }
    

    where you move the content of Sheet1!A1 as a value to Sheet2!B1