title: Script to download a range of cells in google sheet as PDF to local computer and other automation scripts?
body: <p>I have made a invoice format in g sheet as below:
  <a href="https://i.stack.imgur.com/nlHc4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nlHc4.png" alt="enter image description here"></a></p>
  
  <p>In the cell B8:C8 (below Invoice To), there is a dropdown which gets data from client sheet.</p>
  
  <p>What is requierd?</p>
  
  <blockquote>
    <ol>
    <li>To make PDF of selected cells. I want to later on add some buttons and assign macros/script to them and g sheet print action prints everything on the sheet. So need to print only the invoice section (range-A1:G46). Sheet name to be client name + Invoice Date(F8).</li>
    </ol>
  </blockquote>
  
  <p>I have used some script from stack over flow, but they are adding margins. What I want is a full bleed pdf which I am able to get if I use print (ctrl+p) button. </p>
  
  <blockquote>
    <ol start="2">
    <li>To save the invoice data to 'Invoice Register' sheet to track the invoice and payments against them. This is also achieved.</li>
    </ol>
  </blockquote>
  
  <p>I am able to achieve this by recording a macro using relative references.</p>
  
  <pre><code>    function Untitledmacro1() {
    var spreadsheet = SpreadsheetApp.getActive();
  
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Invoice Register'), true);
    spreadsheet.getRange('A1').activate();  
    spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();  
    spreadsheet.getCurrentCell().offset(1, 0).activate();
    spreadsheet.getCurrentCell().setFormulaR1C1('=R[-1]C[0]+1');
  
    spreadsheet.getCurrentCell().offset(0, 1).activate();
  
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Invoice Register'), true);
    spreadsheet.getRange('Invoice!E8').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
  
    spreadsheet.getCurrentCell().offset(0, 1).activate();
  
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Invoice Register'), true);
    spreadsheet.getRange('Invoice!F8').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
  
    spreadsheet.getCurrentCell().offset(0, 1).activate();
  
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Invoice Register'), true);
    spreadsheet.getRange('Invoice!B8:C8').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
  
    spreadsheet.getCurrentCell().offset(0, 2).activate();
  
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Invoice Register'), true);
    spreadsheet.getRange('Invoice!F16').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  };
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/CQNSx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CQNSx.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <ol start="3">
    <li>Once the invoice data is saved and pdf is generated with specific cell range, I want to clear the data in certain fields. This is also achieved.</li>
    </ol>
  </blockquote>
  
  <pre><code>function clearRange() {
    //replace 'Sheet1' with your actual sheet name
    var sheet = SpreadsheetApp.getActive().getSheetByName('Invoice');
  
    sheet.getRange('B8:C8').clearContent(); // client name
  
    sheet.getRange('C12:F15').clearContent(); // Invoice items - description, date/period, amount
  }
  </code></pre>
  
  <blockquote>
    <ol start="4">
    <li>Add 1 number to invoice number. The invoice number format is today's date+current Month+invoice number so far/year (eg: 06111/1920). So we need to add 1 to 5th character for next invoice.</li>
    </ol>
  </blockquote>
  
  <p>I have used below formula and achieved this:</p>
  
  <pre><code>=CONCATENATE(0, DAY(F8),MONTH(F8), COUNTA('Invoice Register'!B2:B99)+1,"/",21920)
  </code></pre>
  
  <p>But would love to make a script for this also.</p>
  
  <p>Basically I had made a invoice tool in excel using VBA and now biggest problem is whenever I export PDF from excel it is showing white margins on right irrespective of troubleshooting.
  <a href="https://i.stack.imgur.com/sTyAx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sTyAx.png" alt="enter image description here"></a></p>
  
  <p>I guess the buttons are obvious and whenever I press 'New Invoice', Invoice No. is increased by 1.</p>
  
  <p>Sorry for the lengthy post as I had to make it as clear as possible and I am totally new to google app scripts. Any help is greatly appreciated and thanks.</p>