title: Google Apps Script: Trouble Passing URL Params from doGet() to doPost()
body: <p>I'm having trouble getting a variable passed from <code>doGet()</code> into <code>doPost()</code>. When the variable <code>sheetName</code> is defined globally without any logic to define it, it works fine, but when I use <code>doGet()</code> to define it using a URL parameter, it won't load the form submission data into my Google Sheet. Basically I'm trying to define the sheet name so I can reuse the code for separate tabs inside the same sheet. Changing <code>sheetName</code> to 'Sheet2' works fine and passes the data into that sheet (assuming the headers are setup properly). I feel like it might be a load order where <code>doGet()</code> is getting called after <code>doPost()</code> or something? Any help would be greatly appreciated.</p>
  
  <p>The base of the code I'm using was taken from here: <a href="https://forum.webflow.com/t/how-to-submit-html-form-directly-to-google-sheets/71432" rel="nofollow noreferrer">https://forum.webflow.com/t/how-to-submit-html-form-directly-to-google-sheets/71432</a></p>
  
  <p>my <code>scriptURL</code> is something like this: <code>https://script.google.com/macros/s/KEY/exec?formUsed=Sheet1</code></p>
  
  <p>My modified <code>Code.gs</code> code is below:</p>
  
  <pre><code>var sheetName;
  
  function doGet(request){
    sheetName = request.parameter.formUsed;
    return ContentService.createTextOutput(sheetName);
  }
  
  //var sheetName = 'Sheet1';
  var scriptProp = PropertiesService.getScriptProperties();
  
  function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet()
    scriptProp.setProperty('key', doc.getId())
  }
  
  function doPost(e) {
    var lock = LockService.getScriptLock()
    lock.waitLock(10000)
  
    try {
      var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
      var sheet = doc.getSheetByName(sheetName)
      var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
      var nextRow = sheet.getLastRow() + 1
      var newRow = headers.map(function(header) {
        return header === 'timestamp' ? new Date() : e.parameter[header]
      })
  
      sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
      return ContentService.createTextOutput(JSON.stringify({
        'result': 'success',
        'row': nextRow
      })).setMimeType(ContentService.MimeType.JSON)
    } catch (e) {
      return ContentService.createTextOutput(JSON.stringify({
        'result': 'error',
        'error': e
      })).setMimeType(ContentService.MimeType.JSON)
    } finally {
      lock.releaseLock()
    }
  }
  </code></pre>