下拉验证脚本不起作用

时间:2018-11-21 08:55:09

标签: validation

我想在修改脚本方面寻求帮助。该脚本负责F和D列的时间戳记,还负责G列的下拉验证。

会发生什么事,当我在F列中选择一个主题时,时间戳将显示在H列中(开始),并且还将显示与所选主题相对应的验证。如果票证URL粘贴在D列(票证URL)上,则时间戳记将显示在I列(结束)中。

这是我的样本表:https://docs.google.com/spreadsheets/d/1CSmpqN01a_Y4XOXwpclEaEnkT-7Lx9QdM7hV4XXcrPM/edit#gid=1087363288

这是我已经修改了几天的脚本。

function onEdit(e) {

    var sheet, cols, colInd, offset, format;   
    sheet = e.source.getActiveSheet();
    cols = [6,4,6];
    colInd = cols.indexOf(e.range.columnStart);
    if (colInd == -1) return;
    if (colInd < 3) {
        offset = [2, 5]
        format = colInd == 0 ? "HH:mm:ss" : "HH:mm:ss";
        e.range.offset(0, offset[colInd])
        .setValue(Utilities.formatDate(new Date(), "GMT+8", format));
    } else {
        setValidation(e);
    }
}

function setValidation(e) {

    var allValues, list,

        /*'easy to change' variables*/
        sheetWithLists = 'Validation',
        rangeWithLists = 'A1:Y', //assuming the first row to be the header row
        secondValidationOffset = 1; //the offset from the above column / the column with the 'second' validation

    /*check conditions*/
    if (e.range.rowStart < 1 || typeof e.value == 'object') return;
    Logger.log('passed')
    /*get all values from the sheet with the lists (cached after the first run)*/

    allValues = getFromCache(sheetWithLists, rangeWithLists)
    Logger.log(allValues)
    Logger.log(allValues[0].indexOf(e.value));
    /*get the correct list(column) and remove the header*/
    list = allValues.map(function (v, i) {
        return v[allValues[0].indexOf(e.value)]
    })
        .splice(1);
    Logger.log(list)
    /*set the validation in offset column*/
    e.range.offset(0, secondValidationOffset)
        .setDataValidation(SpreadsheetApp.newDataValidation()
            .requireValueInList(list)
            .build());
}

function testCache() {
 var sheetWithLists = 'Validation';
 var rangeWithLists = 'D1:BF';//assuming the first row to be the header row
 Logger.log(getFromCache(sheetWithLists, rangeWithLists))
 }
function getFromCache(sheetName, range) {

    var key = 'DD_' + sheetName,
        c = CacheService.getPublicCache(),
        d,
        t = c.get(key);
    if (t) {
        d = JSON.parse(t);
    } else {
        d = SpreadsheetApp.getActiveSpreadsheet()
            .getSheetByName(sheetName)
            .getDataRange()
            .getValues();
        c.put(key, JSON.stringify(d));
    }
    return d;
}

请让我知道为实现这一目标需要进行哪些更改。这是触发器:

  1. F列(主题)将触发开始时间,还将显示子主题(我可以使时间戳生效)
  2. D列(机票URL)将触发结束时间。 (我可以使时间戳生效)

现在唯一缺少的是数据验证

我可以在脚本编写方面使用一些帮助。

提前谢谢!

0 个答案:

没有答案