0

I have just simplified my question with hopes that it will be more understandable. I need the form to update its amount automatically when user selects a product code. And all the reference number to fill automatically with the uuid from the backend. This is my first question on stack, i really hope someone can help.

Amount FIELD >> to be updated based on product code from sheet Reference FIELD >> Get uuid and input it in the field.

SPREADSHEET LINK : https://docs.google.com/spreadsheets/d/1CK1bAcrTAOabkzK0YxliVFFh8VFPYIYXDy6ocvnGuGY

Code.js


 const APP_NAME = "PRODUCT SALES APP"
 var url="https://docs.google.com/spreadsheets/d/1CK1bAcrTAOabkzK0YxliVFFh8VFPYIYXDy6ocvnGuGY/edit#gid=1312844985";
/**
 * Link the html file to another one
 */
function link(filename){
  return HtmlService.createHtmlOutputFromFile(filename).getContent()
}

/**
 * Handle get request
 */
function doGet() {
  return HtmlService.createTemplateFromFile("index.html").evaluate().setTitle(APP_NAME).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
}

function getCost(Productcode){

  var ss= SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("Products");
  var data = ws.getRange(1, 1, ws.getLastRow(), 6).getValues();
  //Logger.log(data);

  var codesList = data.map(function(r){ return r[2]; });
  var costList = data.map(function(r){ return r[4]; });

  var position = codesList.indexOf(Productcode);
  if (position > -1){
    return "₦" + costList[position].toFixed(2);
  } else{
    return 'Unavailable'
  }
}

/**
 * Handle post request
 */
function doPost(e){
  const postData = e.parameter
  const uuid = Utilities.getUuid()
  //const image = createFile(postData.data, postData.profile)
  //save to spreadsheet
 
  var ss= SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("PRODUCT SALES");
  ws.appendRow([new Date(),uuid, postData.category, postData.gender_type, postData.product_class, postData.product_code, postData.first_name, postData.last_name, postData.delivery_address, postData.phone_number, postData.email_address, postData.platinum_id])
  const html = `Thanks for your submission! \n${uuid}`
  return HtmlService.createHtmlOutput(html).setTitle("Success!")
}

index.html

          <div>
            <label for="product_code">Product Code</label>
            <select name="product_code" id="productcode" required>
              <option value="" disabled selected>Select Product Code</option>
              <option value="LC1">"Product Code A"</option>
              <option value="LC2">"Product Code A"</option>
              <option value="LC3">"Product Code A"</option>
            </select>
          </div>
          <!-- AMOUNT -->
          <div>
            <label for="amount">Amount</label>
            <input type="text" id="amount" required>
            </div>
          <!-- REFERRENCE-->
          <div>
            <label for="reff">Reference</label>
            <input type="text" id="reff" required>
            </div>
          <!-- Submit button -->
          <div>
            <input type="submit" value="BUY NOW">
          </div>
        </form>
    
        <!-- Link the javascript -->
        <?!= link('js'); ?>
      </body>
    </html>

js.html

    <script>
      const productAmount = document.getElementById("amount")
      const referenceNo= document.getElementById("reff")
      
    document.getElementById("productcode").addEventListener("select",getEstimate);
document.getElementById("fn").addEventListener("input",getUuid);
    
    function getEstimate(){
      var productCode = document.getElementById("productcode").value;
      if(productCode.length > -1){
      
      google.script.run.withSuccessHandler.getCost(Productcode);
    
      document.getElementById("amount").value = cost;
    
    }

function getUuid(){
  const uuid = Utilities.getUuid.getValue()
    
      referenceNo = uuid;
    
    }
        
    </script>
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Welcome to [so]. Please replace the code by a [mcve] and add a brief description of the link spreadsheet direclty into the quesiton body. – Rubén Dec 01 '22 at 06:19
  • Hi Ruben, i have just updated my question with hope that you can review it again. thanksss! – Yomi Platinum Dec 01 '22 at 13:23
  • I rolled back the question to a previous revision because on this site questions that already have an answer should not be changed in such way that invalidate the answers. Instead if you need clarification, add a comment to the answer, if you need to make a follow up question, post a new question. – Rubén Dec 04 '22 at 17:59

1 Answers1

0

The Google Apps Script services, including Utilities, can't be directly called from client-side JavaScript. On the server side code (.gs files) declare functions that call these services and call them by using google.script.run and withSuccessHandler to call a callback (a JavaScript function passed as parameter) to handle the server side function output. For details see https://developers.google.com/apps-script/guides/html/communication.

P.S. The code have some errors, first spend some time learning about client - server communication, then create a more basic "mcve" an learn how to debug Google Apps Script web apps.

Related (most recent first)

References

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Okay i will work on this and respond back to you. I know i can get this done! – Yomi Platinum Dec 01 '22 at 21:03
  • Hi Ruben, i have done corrections on the web app project, testing the variable/object on the server side and confirming it works, but it just won't pass them to the client-side html. I have given this over a hundred shot. – Yomi Platinum Dec 04 '22 at 13:35
  • Hello Ruben i have been trying to submit another question. The button won't click. Is there a problem with my account? did i violate a rule? or am i temporarily being restricted? – Yomi Platinum Dec 08 '22 at 02:20
  • I have no way to know if you have a partial restriction like a question ban. If you have a ban you should get a message saying that. For details see https://stackoverflow.com/help/question-bans. If you haven't a ban, try using modern web browser in private / incognito / safe mode . – Rubén Dec 08 '22 at 02:29
  • I will do that right away! – Yomi Platinum Dec 08 '22 at 21:15
  • Hey Ruben i have been able to fix it all! thanks. You can preview and tell me what upgrade is best for me because i am trying to use this on my wordpress website. I feel there should be a better approach to this?! Your idea will be appreciated sir (https://script.google.com/macros/s/AKfycbyCgTNm_tXjexZqdgk5TJ1W1vpky501DIoSJLsrK1qvcTge08PtbfNgGC1lEz2nXRj5/exec) – Yomi Platinum Dec 10 '22 at 04:36