Home > Back-end >  Very New to Apps Script, question about why variable isn't showing up
Very New to Apps Script, question about why variable isn't showing up

Time:04-26

I am very new to programming in general, but excited about what I've been able to do so far for work projects. I'm wondering why in the program below, the variable companyID is not populating into the template literal string. I've tried putting it inside and outside the loop, and while I am not getting an error message, it just comes out as a space.

  let activeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('foreachloop');
  let range = activeSheet.getRange(1, 1, 5, 4);
  let values = range.getValues();

//FOR EACH LOOP

values.forEach(function(value, index){
  
let companyID = activeSheet.getRange(1, 9).getValue();

if (index ===0) return;
let descriptionVariable = `Employee Number ${companyID} ${value[0]} ${value[1]} has a current status of ${value[2]}`

activeSheet.getRange(index   1, 4).setValue(descriptionVariable);

})
}```

CodePudding user response:

  1. check your runtime environment. It must be set to V8 if you want to use this feature. See https://developers.google.com/apps-script/guides/v8-runtime
  2. check the content of the cell you are pulling companyID from. Is it empty? You can console.log(companyID) or run a debugger to check

CodePudding user response:

Try it this way:

function lfunko() {
  const sh = SpreadsheetApp.getActive().getSheetByName('foreachloop');
  const values = sh.getRange(1, 1, 5, 4).getValues();
  const companyID = sh.getRange(1, 9).getValue();
  values.forEach(function (value, index) {
    if (index > 0) {
      sh.getRange(index   1, 4).setValue(`Employee Number ${companyID} ${value[0]} ${value[1]} has a current status of ${value[2]}`);
    }
  });
}
  • Related