2015-11-20 221 views
0

我正在尝试在Google Spreadsheets中为特定工作表编写代码,该代码已设置为在特定工作表上工作,可以使用数组完成此工作,还是会在此处创建问题是我有:Google电子表格中的时间戳

原始代码:

function onEdit() { 
    // writes the current date to the last cell on the row when a cell in a specific column is edited 
    // adjust the following variables to fit your needs 
    var sheetNameToWatch = "Sheet1"; 
    var columnNumberToWatch = 20; 

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

    if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch) { 
    var targetCell = sheet.getRange(range.getRow(), sheet.getLastColumn()); 
    targetCell.setValue("" + Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd")); 
    } 
} 

氏是我一直想(目前没有工作)

function onEdit() { 
    var repArray = new Array(); 
    var allSheets = ss.getSheets(); 
    for (i in allSheets) {        
    if ((allSheets[i].getName()).match(/.*?\.$/)) 
     {repArray.push(allSheets[i].getName());} 
} 
    var sheetArray = []; 
    for (var j in repArray) { 
    var tempSheet = ss.getSheetByName(repArray[j]); 
    var sheetNameToWatch = "tempSheet"; 
    var columnNumberToWatch = 11 

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

    if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch) { 
    var targetCell = sheet.getRange(range.getRow(), 1); // I choose 1 because the Date Stamp should be placed in Column A 
    targetCell.setValue("" + Utilities.formatDate(new Date(), "GMT", "dd-MM-yy")); 
    } 
} 

此电子表格是由不同的销售代表的工作在每个有表格中有他们的名字(最后还有一段时间来构建数组),所以当有人更新列K时,我希望datestamp被放置在它们各自的表格的列A上,但目前为止我还没有完成它。

回答

1

是的,我认为这是可能的。这是一些工作代码。我不完全确定你想用正则表达式找到什么,所以你需要改变这条线。

function onEdit() { 

    //declarations 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var allSheets = ss.getSheets(); 
    var regExp = "/.*?\.$/"; 
    var repArray = new Array(); 
    var columnNumberToWatch; 
    var sheet; 
    var range; 
    var targetCell; 

    //used my own regex for testing 
    //note sure what your expression needed to search for 
    regExp = "name" 

    //create array of sheets that match reg-exp 
    for (var i in allSheets) {        
    if ((allSheets[i].getName()).match(regExp)) { 
     repArray.push(allSheets[i].getName()); 
    } 
    } 

    //if a rep sheet was changed in a certain column then 
    //put the current date in column A in the last used row 
    columnNumberToWatch = 11; 
    sheet = ss.getActiveSheet(); 
    range = sheet.getActiveCell(); 
    // array.indexOf(string) returns the index if the string is found 
    // if the string is not found it will return a value of -1 
    if (repArray.indexOf(sheet.getName()) > -1 && range.getColumn() === columnNumberToWatch) { 
    targetCell = sheet.getRange(sheet.getLastRow(), 1); 
    targetCell.setValue(Utilities.formatDate(new Date(), "GMT", "dd-MM-yy")); 
    } 

    //the function will exit at the return statement; 
    return; 
    //old code with the for loop was left in case you want it 
    //the code was modified so it works now 

    var sheetNameToWatch; 
    //loop through sheets in rep-array 
    for (var j in repArray) { 
    sheetNameToWatch = ss.getSheetByName(repArray[j]).getName(); 
    columnNumberToWatch = 11; 
    sheet = ss.getActiveSheet(); 
    range = sheet.getActiveCell(); 
    Logger.log(sheetNameToWatch+columnNumberToWatch+sheet.getName()+range.getA1Notation()); 
    //if a rep sheet was changed in a certain column then 
    //put the current date in column A in the last used row 
    if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch) { 
     targetCell = sheet.getRange(range.getRow(), 1); 
     targetCell.setValue(Utilities.formatDate(new Date(), "GMT", "dd-MM-yy")); 
    } 
    } 
} 
+0

嘿帕特里克纽格鲍尔我试过的代码,但它不工作...它有点不会做任何事情,并且...什么表情正在寻找是包括期间的任何表,如果没有时间然后再没有必要将表添加到数组 –

+0

酷的工作原理是这是它应该做的日期应该在第一列进行更改 –

相关问题