[英]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.