多张OnEdit谷歌脚本在同一张表中 - 另外它们一起工作,但不是很好

时间:2017-10-13 19:30:38

标签: google-apps-script google-sheets

我将这些脚本分开工作但是当我将它们组合起来或创建两个不同的脚本时,只有一个脚本可以运行。如果发现条件格式化工具正在使我的电子表格陷入困境,那么我使用第一个脚本来替换该功能,这就完全不同了。第二个脚本只是一个存档脚本,用于将行移动到存档表。无论我将它们组合在一起,每次只能运行一个。我尝试了两个单独的脚本,我尝试了组合。包括更改函数的名称等。我错过了什么?

function onEdit(e) {
if (e) { 
    var ss = e.source.getActiveSheet();
    var r = e.source.getActiveRange(); 

        // E.g. status column is 2nd (B)
        status = ss.getRange(r.getRow(), 2).getValue();

        // Specify the range with which You want to highlight
        // with some reading of API you can easily modify the range selection properties
        // (e.g. to automatically select all columns)
        rowRange = ss.getRange(r.getRow(),3,1,8);

        // This changes font color
        if (status == 'Complete') {
            rowRange.setFontColor("#d3d3d3");
            rowRange.setBackgroundColor("#FFFFFF");
        } else if (status == 'This Week') {
            rowRange.setFontColor("#000000");
            rowRange.setBackgroundColor("#ACE5A2");
        } else if (status == 'Next Week') { 
            rowRange.setFontColor("#000000");
            rowRange.setBackgroundColor("#D3D8D9");
        } else if (status == 'Priority') { 
            rowRange.setFontColor("#FFFF11");
            rowRange.setBackgroundColor("#1997F6");
        } else if (status == 'Late') { 
            rowRange.setFontColor("#D92B14");
            rowRange.setBackgroundColor("#FFFFFF");
        } else if (status == '') { 
            rowRange.setFontColor("#000000");
            rowRange.setBackgroundColor("#FFFFFF");
        }   
    }
}
function onEditArchive() {

var sheetsToWatch= ["Valley","DBWC","Genus Heart","Mutli 
Run","ECM","MRE","Medella","Mitro"];


var columnNumberToWatch = 10; // column A = 1, B = 2, etc.
var valueToWatch = "Archive";
var sheetNameToMoveTheRowTo = "Archive";

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();


if (sheetsToWatch && range.getColumn() == columnNumberToWatch && 
range.getValue() == valueToWatch) {

var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 
1);
    sheet.getRange(range.getRow(), 1, 1, 
sheet.getLastColumn()).moveTo(targetRange);
    sheet.deleteRow(range.getRow());
  }
}

1 个答案:

答案 0 :(得分:1)

得到Two OnEdit functions not working together

的答案

简而言之,我将第二个OnEditArchive重命名为OnEdit2,然后转到Edit>当前项目触发并设置要在编辑时触发的每个OnEdit。

希望有所帮助!