函数无法从父函数获取价值-Google Apps脚本

时间:2018-08-11 14:52:19

标签: google-apps-script

我有表格,标题是项目编号,左列是工作人员名称,我正在添加代码以开始和停止项目中每个工作人员的工作。 因此,当用户编辑合适的矩形时,它将添加有关该工作人员和项目的新记录(入或出)。

我对名为area()的函数有问题,它没有从onEdit(e)函数获取范围值。

仅当我将值放入area()函数本身时才起作用,例如,当我将

放入时

var vrange =“ S10”

或者我以固定值运行,例如area(“ S10”)

在其他功能正确获取范围变量值时未获取范围变量值

但是根据用户编辑单元格,范围值是动态的。

该行仅针对area(range)函数出现问题,其他函数诸如getProject(range)正确获取范围值。

addRecord(getProject(range),getWorker(range),new Date(),"un",area(range))

希望有人能知道我的错误。

这是代码

function onEdit(e) {
  var range = e.range

  Logger.log("e.range" + range)

  var Currentsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName()

  if (Currentsheet == TimeInterface()) {


    addRecord(getProject(range), getWorker(range), new Date(), "un", area(range))
  }

}


function area(vrange) {
  // var frange = "S10"
  //var frange = vrange
  Logger.log("vrange is " + vrange)
  var currentrow = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(vrange).getRow()
  var currentcol = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(vrange).getColumn()
  var outColumn = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(RecordSettingsSheet()).getRange("B1").getValue()
  var recordingFirstColumn = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(RecordSettingsSheet()).getRange("B2").getValue()
  var recordingLastColumn = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(RecordSettingsSheet()).getRange("B3").getValue()
  var recordingFirstRow = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(RecordSettingsSheet()).getRange("B4").getValue()
  var recordingLastRow = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(RecordSettingsSheet()).getRange("B5").getValue()
  var answer = ""

  if (currentcol == outColumn && currentrow >= recordingFirstRow && currentrow <= recordingLastRow) {
    answer = "out"
    return answer
  } else if (currentrow >= recordingFirstColumn && currentrow <= recordingLastColumn && currentrow >= recordingFirstRow && currentrow <= recordingLastRow) {
    answer = "in"
    return answer

  } else {
    answer = "Error"
    return answer
  }
}

function TimeInterface() {
  var TimeInterfacev = "Interface"
  return TimeInterfacev
}

function RecordSettingsSheet() {
  var recordSettingsSheet = "Record Settings"
  return recordSettingsSheet
}



function getProject(range) {
  var ProjectID = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(1, range.getColumn()).getValues()
  Logger.log("getProject range is " + range)

  return ProjectID
}

function getWorker(range) {
  var WorkerID = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(range.getRow(), 1).getValues()
  return WorkerID
}



function setValue(cellname, Value) {
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Sheetname()).getRange(cellname).setValue(Value);
}


function getValue(cellname) {
  return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Sheetname()).getRange(cellname).getValue();


}

function Sheetname() {
  var Sheet = "TimeRecords"
  return Sheet
}

function newRowNumber() {
  var row = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Sheetname()).getLastRow() + 1;
  return row
}

function addRecord(a, b, c, d, e) {

  var row = newRowNumber();
  var aa = getValue("A" + (row - 1))
  var bb = getValue("B" + (row - 1))
  var cc = getValue("C" + (row - 1))
  var dd = getValue("D" + (row - 1))

  if (
    a == aa && b == bb && d == dd
  ) {


  } else {


    setValue("A" + row, a);
    setValue("B" + row, b);
    setValue("C" + row, c);
    setValue("D" + row, d);
    setValue("E" + row, e);
  }
}

这是错误的详细信息

{
 insertId:  "ofc7bcfvzisc2"  
 jsonPayload: {
  context: {
   reportLocation: {
    filePath:  "Code"     
    functionName:  "area"     
    lineNumber:  22     
   }
  }
  message:  "Range not found
    at area(Code:22)
    at onEdit(Code:12)
"   
  serviceContext: {
   service:  "AKfycbyZxuwklLCadxRO93yHKQH0HO34bM9fReTelwt2I39_"    
  }
 }
 labels: {
  script.googleapis.com/process_id:  "EAEA1GOx_xjeztWsGim9PR3gYwydfe6FUt9_U2QNlaA6mIDJNtAm21jdbcWIBqG3z1Hg9CPQuaS_Hf5tJYJh8XqtqDQvgwfh1AX6DQ1M5Jc5tfPEOrgRj7eM4-ueK9m5FidEZBs31l3e-MsG9aHj4JLFEcsntzvvPMZB20Q"   
  script.googleapis.com/project_key:  "MvVaniAhXvNj_E5qpMKyh9JCBc1Z0ol9u"   
  script.googleapis.com/user_key:  "AJw2Yz+oU8zemc1ioio5IJNfc43E3QhuOpzprTTJO/RULzh/3VQSMjron9r4W4J+nuGW9Hj2YCF1"   
 }
 logName:  "projects/project-id-5448248979683187508/logs/script.googleapis.com%2Fconsole_logs"  
 receiveTimestamp:  "2018-08-11T15:29:53.172475577Z"  
 resource: {
  labels: {
   function_name:  "onEdit"    
   invocation_type:  "event"    
   project_id:  "project-id-5448248979683187508"    
  }
  type:  "app_script_function"   
 }
 severity:  "ERROR"  
 timestamp:  "2018-08-11T15:29:52.164Z"  
}

1 个答案:

答案 0 :(得分:0)

我使用

解决了问题

getRange(range.getRow(), range.getColumn()).getRow() 

代替

getRange(range).getRow();