google-apps-scriptgoogle-sheetsgoogle-sheets-macros

Send values from file with link


I'm here to ask you things about Google Sheets macro,
I want to send information from Google Sheet file to another.
I wanted, when I click on the link, that it takes and send the values from the same row to the other

file.


Solution

  • Just paste into code.gs and run showSideBar function. Pay attention to the instructions provided in the top of the sidebar and everything should work just fine.

    function sendCurrentRow() {
      var tssid='target spreadsheet id';//put the target spreadsheet id here
      const ss=SpreadsheetApp.getActive();
      const sh=ss.getActiveSheet();
      const rg=sh.getActiveRange();
      const row=sh.getRange(rg.getRow(),1,1,sh.getLastColumn()).getValues();
      const tss=SpreadsheetApp.openById(tssid);
      var tsh=tss.getSheetByName(sh.getName());
      if(!tsh) {
        var tsh=tss.insertSheet(sh.getName())
      }
      tsh.getRange(rg.getRow(),1,1,sh.getLastColumn()).setValues(row);  
    }
    
    function showSideBar() {
      var html='<p>If you haven put you target spreadsheet id in function sendCurrentRow() then do before pressing button below.</p><p>Place cursor on the row you wish to send and click button below.</p><br />'
      html+='<input type="button" value="Send" onClick="google.script.run.sendCurrentRow()" />';
      SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutput(html).setTitle('Send Current Row'));
    }