0

我剛開始在Google文檔中探索這些電子表格腳本。我想編寫一個腳本來查找項目之間的日期重疊(將給定單元格的bg顏色更改爲紅色),並創建一個新列以顯示該項目類型的衝突數。如果你能給我提供一些例子或者一種方法,我會非常感激。Google電子表格中的重疊日期格式

這是我的數據集。

Data

我想這是什麼。這隻適用於第一列。

function formatting() { 
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); // get the sheet 
    var columnF = sheet.getRange(1, 6, sheet.getLastRow(), 1).setBackgroundColor('white'); // get all the rows and clear colors 
    var columnG = sheet.getRange(1, 7, sheet.getLastRow(), 1).setBackgroundColor('white'); // get all the rows and clear colors 
    var fValues = columnF.getValues(); // get the values 
    var gValues = columnG.getValues(); 
    var day = 24*3600*1000 
    Logger.log(gValues) 
    var startDay1 = parseInt(fValues[0][0].getTime()/day) 
    var endDay1 = parseInt(gValues[0][0].getTime()/day) 
    var startDay2 = parseInt(fValues[1][0].getTime()/day) 
    var endDay2 = parseInt(gValues[1][0].getTime()/day) 
    if (startDay1<endDay2 && startDay2<endDay1) {sheet.getRange(1, 6, 1, 1).setBackgroundColor('red')} 
    else {sheet.getRange(1, 6, 1, 1).setBackgroundColor('green')} 
    } 
+1

歡迎來到SO。在大多數情況下,除非你有我們迄今爲止所做的工作,否則我們不會爲你工作。請提供。 – Brian

+1

歡迎來到SO,請在發佈之前閱讀[FAQ](http://stackoverflow.com/help)...您的問題不是編程問題,而是直接代碼請求。*詢問代碼的問題必須證明最小化了解正在解決的問題。包括嘗試的解決方案 –

+1

@Brian :-)同時... –

回答

0

這裏是答案,如果任何人有興趣。

function formatting(m, d) { 
    try { 
     var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); // get the sheet 
     var output = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2'); // output sheet   
     output.clear() 

     // Going to need the column for project type 
     var counts = {}; 
     var eValues = sheet.getRange(1, 3, sheet.getLastRow(), 1).getValues(); 
     var columnF = sheet.getRange(1, 1, sheet.getLastRow(), 1).setBackgroundColor('white'); // get all the rows and clear colors 
     var columnG = sheet.getRange(1, 2, sheet.getLastRow(), 1).setBackgroundColor('white'); // get all the rows and clear colors 
     var columnD = sheet.getRange(1, 4, sheet.getLastRow(), 1).setBackgroundColor('white').clearContent(); // get all the rows and clear colors 
     var columnCritical = sheet.getRange(1, 7, sheet.getLastRow(), 1).setBackgroundColor('white').clearContent(); // get all the rows and clear colors 
     var fValues = columnF.getValues(); // get the values 
     var gValues = columnG.getValues(); 
     var day = 24 * 3600 * 1000 


     // loop through all the rows in the dataset 


     for (var r = 0; r < (fValues.length - 1); r++) { 

       var startDay1 = parseInt(fValues[r][0].getTime()/day); 
       var endDay1 = parseInt(gValues[r][0].getTime()/day); 

       // loop through all the rows for given date 

       for (var z = 0; z < (fValues.length - 1); z++) { 

        var startDay2 = parseInt(fValues[(z)][0].getTime()/day); 
        var endDay2 = parseInt(gValues[(z)][0].getTime()/day); 

        // if the date is the same go to the next one 
        if (startDay1 == startDay2 && endDay2 == endDay1) continue; 

        // check for conflicts 
        else if (startDay1 < endDay2 && startDay2 < endDay1) { 

         //Here is our conflict!!!; 



         var projectn = r + 1 
         if (counts[projectn] !== undefined) { // strict(!) comparison 
          // add one to this projects count 
          counts[projectn] += 1; 
         } else { 
          // create the first count for this project 
          counts[projectn] = 1; 
         } 
        } else { 
        // if there is no conflict 
        } 
       } //end of for loop for var z 

       // change the background of the counts to red and set values 
       if (counts[r + 1] == undefined) { 
        sheet.getRange(r + 1, 4, 1, 1).setValue(counts[r + 1]).setBackground('green'); 
       } else { 
        sheet.getRange(r + 1, 4, 1, 1).setValue(counts[r + 1]).setBackground('red'); 
       } 
     } // end of for loop for var r 


     // show if there is any errors 
    } catch (e) { 
     Logger.log(e.lineNumber + ' - ' + e); 
    } 
} 
1

循環遍歷每行所需的代碼。不知道你想如何與上一個項目抗衡,因爲沒有日期來比較它。

保持標記爲紅色的項目計數的簡單方法是創建一個javascript對象(項目),並存儲每個項目及其計數。這裏是關於JavaScript對象的一些文檔:Javascript.info - Objects

function formatting() { 
    try{ 
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); // get the sheet 

    // Going to need the column for project type 
    var projects = {}; 
    var eValues = sheet.getRange(1, 5, sheet.getLastRow(), 1).getValues(); 

    var columnF = sheet.getRange(1, 6, sheet.getLastRow(), 1).setBackgroundColor('white'); // get all the rows and clear colors 
    var columnG = sheet.getRange(1, 7, sheet.getLastRow(), 1).setBackgroundColor('white'); // get all the rows and clear colors 
    var fValues = columnF.getValues(); // get the values 
    var gValues = columnG.getValues(); 
    var day = 24*3600*1000 
    Logger.log(gValues) 
    // loop through all the rows in the dataset 
    for(var r = 0; r < (fValues.length - 1); r++){ 
     var startDay1 = parseInt(fValues[r][0].getTime()/day); 
     var endDay1 = parseInt(gValues[r][0].getTime()/day); 
     var startDay2 = parseInt(fValues[(r+1)][0].getTime()/day); 
     var endDay2 = parseInt(gValues[(r+1)][0].getTime()/day); 
     if (startDay1<endDay2 && startDay2<endDay1) { 
     sheet.getRange((r+1), 6, 1, 1).setBackgroundColor('red'); 
     var projectName = eValues[r][0]; 
     if(projects[projectName] !== undefined) { // strict(!) comparison 
      // add one to this projects count 
      projects[projectName] += 1; 
     }else{ 
      // create the first count for this project 
      projects[projectName] = 1; 
     } 
     } 
     else { 
     sheet.getRange((r+1), 6, 1, 1).setBackgroundColor('green'); 
     } 
    } 

    // updating is done, need to create the counts 
    // shove the results in column L 
    var rowCount = 1; 
    for(var key in projects) { 
     var val = projects[key]; 
     sheet.getRange(rowCount, 12, 1, 1).setValue(key+": "+val); 
     rowCount += 1; 
    } 
    }catch(e){ 
    Logger.log(e.lineNumber + ' - ' + e); 
    } 
} 
+0

非常感謝。我有另一個問題。如果我想檢查每行是否與其餘行重疊,該怎麼辦?我的意思是項目的開始和結束日期將保持不變,並且它將遍歷所有行。 – gradLife

相關問題