skip to Main Content

I am making a web app using google sheets and google app script. My web app have an index page and an input page. There is no problem with inputting the data, however when i try to populate the data from google sheet it displayed undefined value for cells that are empty in the google sheet.

My script is quite long so i attached it here:
https://drive.google.com/drive/folders/1Zl-8UsntRUTlJcnaBuYIgXP2PvD4mIyN?usp=sharing

What i want is, when i click the edit button the form will be populated with the right data and if the data in google sheet is empty then it will just display the placeholder.
Thank you in advance.

Screenshot of undefined

2

Answers


  1. To resolve this, you can add a simple check in order to handle empty values.

    When populating the form with data retrieved from the Google Sheet, you can modify the code to check if the value is undefined or empty, and if so, display the placeholder instead.

    In the function where you’re fetching the record to populate the form (getRecordById), add logic to check for undefined or empty values and handle them.

    Here’s a quick example which you can use:

    function getRecordById(id) {
       if (!id || !checkId(id)) {
          return null;
       }
    
       const range = getRangeById(id);
       if (!range) {
          return null;
       }
    
       if (result && result.length > 0) {
          const record = result[0];
    
       for (let i = 0; i < record.length; i++) {
          if (!record[i]) {
             record[i] = ""; // Set the value to an empty string
          }
       }
    
       return record;
    }
    
    return null;
    
    }
    
    Login or Signup to reply.
  2. Try modifying your readRecord function as:

    /**
     * READ RECORD
     * REF:
     * https://developers.google.com/sheets/api/guides/values#read
     */
    function readRecord(range) {
      try {
        let result = Sheets.Spreadsheets.Values.get(SPREADSHEETID, range);
    
        let data = result.values;
        for (let i = 0; i < data.length; i++) {
          for (let j = 0; j < data[i].length; j++) {
            if ( data[i][j] == undefined ) data[i][j] = '';
          }
        }    
        return data;    
        
        //return result.values;
      } catch (err) {
        console.log('Failed with error %s', err.message);
      }
    }  
    

    I get some of your code to keep your coding style reusing part your searchRecords function.

    This is not an elegant solution but, if it works, will give you a hint to found your best solution.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search