简体   繁体   中英

getDisplayValue() and getValue() from a cell returns #VALUE! error most of the time

I have modified a script of a punch in/out system by adding a function that grabs a value from AQ1 cell in Schedule_data sheet and places it in E column of Break_data sheet to last empty row along with other data in separate cells. The problem I'm encountering is that sometimes the cell content are copied over, and sometimes I get #VALUE! error instead. I've tried removing the formula from the referenced cell and only leaving plain text, but got same result. Changing getValue() to getDisplayValue() also hasn't changed the outcome. Clearing the logger after if/else statements hasn't helped either. The error seems to come up without any pattern. The original script still works fine and consistently produces the data in their respective A,B,C,D columns, but I'm afraid I don't know enough about apps script to effectively troubleshoot it.

This is what I have so far:

function cellRange(value) {
return 
SpreadsheetApp.getActive().getSheetByName('Schedule_data').getRange(value).getDisplayValue();
}
function setValue(cellName, value) {
SpreadsheetApp.getActive().getSheetByName('Break_data').getRange(cellName).setValue(value);
}
function getValue(cellName) {
return 
SpreadsheetApp.getActive().getSheetByName('Break_data').getRange(cellName).getValue();
}
function getNextRow() {
return SpreadsheetApp.getActive().getSheetByName('Break_data').getLastRow() + 1;
}
function addRecord(a, b, c, d, e) {
  var row = getNextRow();
  setValue('A' + row, a);
  setValue('B' + row, b);
  setValue('C' + row, c);
  setValue('D' + row, d);
  setValue('E' + row, e);
}

function pause() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Break_data');
var lastRow = sheet.getLastRow();
var setColumn = 2;
var inORout = sheet.getRange(lastRow, setColumn).getValue();
Logger.log(inORout);

  if(inORout == 'Pause'){
    return;
  }
  else
   var formatDate = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "h:mma");
   var fullDate = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "MM/dd/yy");
   setValue('d1', Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "EEEE"));
   addRecord(getValue('d1'),'Pause',  formatDate, fullDate, cellRange('aq1'));
}

function resume() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Break_data');
var lastRow = sheet.getLastRow();
var setColumn = 2;
var inORout = sheet.getRange(lastRow, setColumn).getValue();
Logger.log(inORout);

  if(inORout == 'Resume'){
    return;
  }
  else
   var formatDate = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "h:mma");
   var fullDate = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "MM/dd/yy");
   setValue('d1', Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "EEEE"));
   addRecord(getValue('d1'),'Resume',  formatDate, fullDate, cellRange('aq1'));
}

image of value error

Despite of how it looks in the attached image, the added E column is not associated with the current day of the week or relevant to the A column, but instead displays the starting day of a project which sometimes may last longer than a day.

Here's another way to do the same thing you did:

function pauseOrResume() {
  const opts = ['Pause','Resume'];
  const ss = SpreadsheetApp.getActive();
  const ash = ss.getActiveSheet();
  const sh = ss.getSelection('Break_data')
  const sdsh = ss.getSheetByName('Schedule_data');
  const inORout = sh.getRange(sh.getLastRow(), 2).getValue();
  if (~opts.indexOf(inORout)) {
    return;
  } else {
    const formatDate = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), "h:mma");
    const fullDate = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), "MM/dd/yy");
    sh.getRange('D1').setValue(Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), "EEEE"));
    ash.appendRow([sh.getRange('D1').getValue(), inORout, formatDate, fullDate, sdsh.getRange('aq1').getDisplayValue()])
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM