2016-09-07 17 views
0

Googleスプレッドシートでは、Sheet1には3つの値があります。別のシート値に基づく1シートの動的列 - Googleシート

enter image description here

シート2は2つの事前定義された列(のCol1、Col2に)と時間値に基づいて添付する必要がある動的な列を有する[持続時間は= DATEDIF(開始日、終了日、 "M")により算出されます]。

Example 1: 
StartDate = 01-Sep-2016 and EndDate = 30-Nov-2016 
So the Duration is 2 

enter image description here

Example 2: 
StartDate = 01-Sep-2016 and EndDate = 31-Dec-2016 
So the Duration is 3 

enter image description here

列は動的であるため、それは、列の列と行のインデックスに基づく値の代わりにコード次のように符号化されたハードに設定することが可能です。

function onOpen() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var s1= ss.getSheetByName("Sheet1"); 
    var s2= ss.getSheetByName("Sheet2"); 
    var sDate = s1.getRange("B5").getValue(); 
    var sDuration = s1.getRange("B7").getValue(); 
    var sMonth = Utilities.formatDate(sDate, Session.getScriptTimeZone(), "MMM"); 
    var sYear = Utilities.formatDate(sDate, Session.getScriptTimeZone(), "YYYY"); 
    for (var cell = 1; cell <= sDuration; cell++) { 
     s2.getRange("C1").setValue(sMonth + ", " + sYear); 
    } 
} 

ありがとうございます。

答えて

0

あなたは、溶液の80%を発見し、私はちょうどそれが動的に動作させるためにあなたのコードにいくつかの修正を追加しました:

function myFunction(){ 

    onOpen(); 
} 

function onOpen() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var s1= ss.getSheetByName("Sheet1"); 
    var s2= ss.getSheetByName("Sheet2"); 
    var sDate = new Date(s1.getRange("B1").getValue()); // changed to B1 - the start date value, and sDate must be a Date Object, not string. 
    var sDuration = s1.getRange("B3").getValue(); // changed to B3 - the duration value 
    for (var cell = 0; cell < sDuration; cell++) { // cell starts from 0 
    var sMonth = Utilities.formatDate(sDate, Session.getScriptTimeZone(), "MMM"); // I moved this into FOR loop 
    var sYear = Utilities.formatDate(sDate, Session.getScriptTimeZone(), "YYYY"); // I moved this into FOR loop 
    s2.getRange(1,cell+3).setValue(sMonth + ", " + sYear); // geting range with row & column value, not with Column name 
    sDate.setMonth(sDate.getMonth()+1); // add one month to sDate 
    } 
} 

私は変わったコードのコメントがあります。私はあなたがこれが助けになると思う。

0

コードを少し変更してChange value of cell in same row with google script if column has certain valueになります。

修飾開く時()は、ある

function onOpen() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var s1= ss.getSheetByName("Sheet1"); 
    var s2= ss.getSheetByName("Sheet2"); 
    var sDate = s1.getRange("B5").getValue(); 
    var sDuration = s1.getRange("B7").getValue(); 
    var lastColumn = s2.getLastColumn(); 
    for (var cell = 3; cell <= lastColumn + sDuration; cell++){ 
    var currentCell = s2.getRange(1, cell); 
    currentCell.setValue(Utilities.formatDate(sDate, Session.getScriptTimeZone(), "MMM") + ", " 
        + Utilities.formatDate(sDate, Session.getScriptTimeZone(), "YYYY")); 
    sDate.setMonth((sDate.getMonth() + 1) % 12); 
    } 
} 
関連する問題