简体   繁体   English

Google电子表格脚本,该脚本会在工作表中的列的编辑时触发,并根据值覆盖用户定义的单元格的值

[英]A google spreadsheet script that triggers on edit of a column in a sheet and overwrites the value of a user defined cell or not depending on the value

Hope you are all well. 希望你一切都好。

I am trying to write this nice script for my office because my colleagues lack a bit of discipline. 我正在尝试为我的办公室写一个不错的脚本,因为我的同事缺乏纪律。

This script should trigger if a column in a sheet is edited, preferably, but it's fine if it trigger on sheet edit or maybe even on spreadsheet edit. 最好在工作表中的一列被编辑时触发该脚本,但是如果它在工作表编辑或什至在电子表格编辑时触发就可以。 This script should look at the value in that column of that sheet and if any of them have the strings "On hold(i)","On hold(ii)" or "On hold(iii)" then the cell on another column (chosen by the user) on the same row should be overwritten with the string "TBC". 该脚本应查看该工作表该列中的值,如果其中任何一个具有字符串“ On hold(i)”,“ On hold(ii)”或“ On hold(iii)”,则另一列上的单元格(由用户选择)在同一行上,应使用字符串“ TBC”覆盖。 I tried piecing this from google and below is what I got but since I am here that obviously doesn't work haha. 我试图从谷歌和下面这是我所得到的,但是由于我在这里,显然不起作用哈哈。 Any help would be greatly appreciated !! 任何帮助将不胜感激 !! :(( :((

function OnEdit() {
    var a=1;
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    while ( a<200 ){
        if ( ss.getSheetByName('Active Jobs').getRange(a,12) == "On hold (i)" ) {
            ss.getSheetByName('Active Jobs').getRange(a,15).setValues("TBC");
            a=a+1;
        }
    }
}

I read somewhere on google that naming the funciton OnEdit would make the function trigger if the spreadsheet was edited but it doesn't seem to work. 我在Google上的某个地方读到,如果对电子表格进行了编辑,则命名功能OnEdit将使函数触发,但似乎不起作用。

function OnEdit() {
   var a=1;
   var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  var activeCell = activeSheet.getActiveCell();

  //Check if the sheet is a JOb sheet and the cell us the status cell
  if (activeSheet.getName().indexOf("Job ID") != -1 && activeCell.getRow() == 2 && activeCell.getColumn() == 15){
    var switchValue = activeCell.getValue();
    switch (switchValue){
      case "On hold (i)":
      case "On hold (ii)": 
      case "On hold (iii)":
      case "To be assigned":
        //Write date to active jobs sheet
        addDateToActive("TBC");
        break;
      case "In progress":
        var newDate = Browser.inputBox("Please enter report out date");
        addDateToActive(newDate);
        break;
      default:
        Browser.msgBox("GOTHERE");
    }
  }
}
function addDateToActive(input){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  var activeCell = activeSheet.getActiveCell();
  var jobid = activeSheet.getRange(2,1).getValue().toString();
  var activeJobSheet = ss.getSheetByName("Active Jobs");
  var activeJobs = activeJobSheet.getRange(1,1,activeJobSheet.getLastRow(),1).getValues();
  activeJobs = ColumnToArray(activeJobs);
  var jobrow = activeJobs.indexOf(jobid)+1;
  if (jobrow == -1){
    Browser.msgBox("Job Id not preent on Active Jobs sheet");
  }else{
    activeJobSheet.getRange(jobrow,15).setValue(input);
  }
}

function ColumnToArray(columnData){
  //Ensure that the data supplied is a column
  if(columnData[0].length == 1){
    //Convert column to array
    var dataout = [];
    for (var a = 0; a < columnData.length; a++){
      dataout.push(columnData[a][0]);
    }
  }else{
    throw new Error("Data Supplied to ColumnToArray was not a simple Column");
  }
  return dataout;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google表格脚本根据列单元格中的值锁定或解锁行 - Google Sheets Script to lock or unlock a row depending on the value in the cell of a column Google Sheet Script - 如果找到单元格值,则返回标头值 - Google Sheet Script - Return header value if cell value found 无法使用谷歌电子表格 api v4.0 更新单元格的谷歌工作表值 - Unable to update google sheet value of a cell using google spreadsheet api v4.0 使用谷歌应用程序脚本在电子表格的一列中获取价值 - get value in one column in spreadsheet using google apps script 根据单元格值滚动一行到屏幕顶部:Google Sheet Script - Scroll a row to the top of the screen based on cell value: Google Sheet Script 编辑谷歌表格应用脚本中过滤行中的单元格值 - Edit cell value from filtered row in app script for google sheets 在编辑时将动态单元格的值复制到另一个工作表 - Copy value of a dynamic cell to another sheet on edit Google表格脚本读取单元格“ a”的值并根据单元格“ a”的值写入单元格“ b” - Google Sheet script that reads cell “a” and writes to cell “b” based off cell “a” value 使用onEdit触发器将值返回到Google电子表格中的单元格 - Return a value to a cell in google spreadsheet with an onEdit trigger 在电子表格Google Appscript中增加单元格值 - Increment cell value in spreadsheet Google Appscript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM