Use Google Script to count dates in a column equal to a specific cell












0















I have been using VBA for years, but I have had some extreme difficulty converting to google script and javascript. I am trying to create an invoice number based on invoice date, taking into consideration previous entries with the same date, using the format YYMMDD## (e.g. 3 invoices on 11/13/2018 would create invoice numbers 18111301, 18111302, and 18111303) in Google Sheets. I cannot use a formula for this particular situation.



I have spent the last 3 days digging through posts and GS tutorials without success. I have GOT to be missing something. I know this has to be some quick fix for something seriously easy to do, but I would greatly appreciate some assistance.



In this current iteration, I tried converting the search value and dataset to a Date format, but I have tried it without the conversion. I apologize if the notes seem copious. I start mapping each step when things go wrong.



function testbed0() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var dte = ss.getActiveCell().getValue() //invoice date entered by user in column 1
var cdte = Date(dte); //???convert cell value for comparison to date
var lrow = ss.getLastRow();
var r = ss.getRange(1, 1, lrow, 1); //set range to first column, rows 1 to last with data
var data = Date(r.getValues()); //???convert dataset to date format

var count = 0; //start counter at 0
//compare variable with dataset & increase counter for matches
for (var i = 0; i < data.length ; i++) {
if (data[i][0] == cdte) {
count++;
}
}
return (count);

//parts of invoice number
var invDy = ("0" + (dte.getDate())).slice(-2);
var invMo = ("0" + (dte.getMonth() + 1)).slice(-2);
var invYr = ("0" + (dte.getYear())).slice(-2);
var invCt = ("0" + count).slice(-2);

//enter new invoice number in invoice column of active row
ss.getActiveCell().offset(0, 2).setValue(invYr+invMo+invDy+invCt);
}









share|improve this question























  • In case anyone is wondering, the output for this gives the correct format, but invCt always reads 00.

    – CC_CE
    Nov 13 '18 at 20:43











  • Please add this comment to your post using the edit link

    – Nick
    Nov 13 '18 at 21:54
















0















I have been using VBA for years, but I have had some extreme difficulty converting to google script and javascript. I am trying to create an invoice number based on invoice date, taking into consideration previous entries with the same date, using the format YYMMDD## (e.g. 3 invoices on 11/13/2018 would create invoice numbers 18111301, 18111302, and 18111303) in Google Sheets. I cannot use a formula for this particular situation.



I have spent the last 3 days digging through posts and GS tutorials without success. I have GOT to be missing something. I know this has to be some quick fix for something seriously easy to do, but I would greatly appreciate some assistance.



In this current iteration, I tried converting the search value and dataset to a Date format, but I have tried it without the conversion. I apologize if the notes seem copious. I start mapping each step when things go wrong.



function testbed0() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var dte = ss.getActiveCell().getValue() //invoice date entered by user in column 1
var cdte = Date(dte); //???convert cell value for comparison to date
var lrow = ss.getLastRow();
var r = ss.getRange(1, 1, lrow, 1); //set range to first column, rows 1 to last with data
var data = Date(r.getValues()); //???convert dataset to date format

var count = 0; //start counter at 0
//compare variable with dataset & increase counter for matches
for (var i = 0; i < data.length ; i++) {
if (data[i][0] == cdte) {
count++;
}
}
return (count);

//parts of invoice number
var invDy = ("0" + (dte.getDate())).slice(-2);
var invMo = ("0" + (dte.getMonth() + 1)).slice(-2);
var invYr = ("0" + (dte.getYear())).slice(-2);
var invCt = ("0" + count).slice(-2);

//enter new invoice number in invoice column of active row
ss.getActiveCell().offset(0, 2).setValue(invYr+invMo+invDy+invCt);
}









share|improve this question























  • In case anyone is wondering, the output for this gives the correct format, but invCt always reads 00.

    – CC_CE
    Nov 13 '18 at 20:43











  • Please add this comment to your post using the edit link

    – Nick
    Nov 13 '18 at 21:54














0












0








0








I have been using VBA for years, but I have had some extreme difficulty converting to google script and javascript. I am trying to create an invoice number based on invoice date, taking into consideration previous entries with the same date, using the format YYMMDD## (e.g. 3 invoices on 11/13/2018 would create invoice numbers 18111301, 18111302, and 18111303) in Google Sheets. I cannot use a formula for this particular situation.



I have spent the last 3 days digging through posts and GS tutorials without success. I have GOT to be missing something. I know this has to be some quick fix for something seriously easy to do, but I would greatly appreciate some assistance.



In this current iteration, I tried converting the search value and dataset to a Date format, but I have tried it without the conversion. I apologize if the notes seem copious. I start mapping each step when things go wrong.



function testbed0() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var dte = ss.getActiveCell().getValue() //invoice date entered by user in column 1
var cdte = Date(dte); //???convert cell value for comparison to date
var lrow = ss.getLastRow();
var r = ss.getRange(1, 1, lrow, 1); //set range to first column, rows 1 to last with data
var data = Date(r.getValues()); //???convert dataset to date format

var count = 0; //start counter at 0
//compare variable with dataset & increase counter for matches
for (var i = 0; i < data.length ; i++) {
if (data[i][0] == cdte) {
count++;
}
}
return (count);

//parts of invoice number
var invDy = ("0" + (dte.getDate())).slice(-2);
var invMo = ("0" + (dte.getMonth() + 1)).slice(-2);
var invYr = ("0" + (dte.getYear())).slice(-2);
var invCt = ("0" + count).slice(-2);

//enter new invoice number in invoice column of active row
ss.getActiveCell().offset(0, 2).setValue(invYr+invMo+invDy+invCt);
}









share|improve this question














I have been using VBA for years, but I have had some extreme difficulty converting to google script and javascript. I am trying to create an invoice number based on invoice date, taking into consideration previous entries with the same date, using the format YYMMDD## (e.g. 3 invoices on 11/13/2018 would create invoice numbers 18111301, 18111302, and 18111303) in Google Sheets. I cannot use a formula for this particular situation.



I have spent the last 3 days digging through posts and GS tutorials without success. I have GOT to be missing something. I know this has to be some quick fix for something seriously easy to do, but I would greatly appreciate some assistance.



In this current iteration, I tried converting the search value and dataset to a Date format, but I have tried it without the conversion. I apologize if the notes seem copious. I start mapping each step when things go wrong.



function testbed0() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var dte = ss.getActiveCell().getValue() //invoice date entered by user in column 1
var cdte = Date(dte); //???convert cell value for comparison to date
var lrow = ss.getLastRow();
var r = ss.getRange(1, 1, lrow, 1); //set range to first column, rows 1 to last with data
var data = Date(r.getValues()); //???convert dataset to date format

var count = 0; //start counter at 0
//compare variable with dataset & increase counter for matches
for (var i = 0; i < data.length ; i++) {
if (data[i][0] == cdte) {
count++;
}
}
return (count);

//parts of invoice number
var invDy = ("0" + (dte.getDate())).slice(-2);
var invMo = ("0" + (dte.getMonth() + 1)).slice(-2);
var invYr = ("0" + (dte.getYear())).slice(-2);
var invCt = ("0" + count).slice(-2);

//enter new invoice number in invoice column of active row
ss.getActiveCell().offset(0, 2).setValue(invYr+invMo+invDy+invCt);
}






google-apps-script google-sheets count






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 20:40









CC_CECC_CE

1




1













  • In case anyone is wondering, the output for this gives the correct format, but invCt always reads 00.

    – CC_CE
    Nov 13 '18 at 20:43











  • Please add this comment to your post using the edit link

    – Nick
    Nov 13 '18 at 21:54



















  • In case anyone is wondering, the output for this gives the correct format, but invCt always reads 00.

    – CC_CE
    Nov 13 '18 at 20:43











  • Please add this comment to your post using the edit link

    – Nick
    Nov 13 '18 at 21:54

















In case anyone is wondering, the output for this gives the correct format, but invCt always reads 00.

– CC_CE
Nov 13 '18 at 20:43





In case anyone is wondering, the output for this gives the correct format, but invCt always reads 00.

– CC_CE
Nov 13 '18 at 20:43













Please add this comment to your post using the edit link

– Nick
Nov 13 '18 at 21:54





Please add this comment to your post using the edit link

– Nick
Nov 13 '18 at 21:54












1 Answer
1






active

oldest

votes


















0














This isn't exactly what you asked for but it accomplishes the same thing and perhaps it will help you to move further. I formatted column one to be plain text. This calculates all of the invoice numbers from top to bottom of the data.



function makeInvoiceNumbers(){
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
var rg=sh.getDataRange();
var vA=rg.getValues();
var actRow=sh.getActiveCell().getRow();
for(var i=1;i<vA.length;i++){
var cnt=0;
for(var j=1;j<i;j++){//Working from top to bottom creating invoice numbers
if(vA[i][0]==vA[j][0]){//I assume that the entrees on the top came before the entrees below them.
cnt++;
}
}
vA[i][1]=getInvoiceString(vA[i][0].toString(),cnt);
}
rg.setValues(vA);
}
function getInvoiceString(d,n){
var dA=d.split('/');
var yy=dA[2].toString().slice(2);//gets the last two digits of a four digit year
var mm=padFor2Digits(dA[0]);
var dd=padFor2Digits(dA[1]);
var nn=padFor2Digits(String(n));
return yy+mm+dd+nn;
}
function padFor2Digits(s){
if(s.length<2){
s='0' + s;
}
return s
}


Data before running makeInvoiceNumbers()



enter image description here



Data after running makeInvoiceNumbers()



enter image description here



If I were doing this for myself I would probably use the onEdit trigger and just do it for one line at a time.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289162%2fuse-google-script-to-count-dates-in-a-column-equal-to-a-specific-cell%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This isn't exactly what you asked for but it accomplishes the same thing and perhaps it will help you to move further. I formatted column one to be plain text. This calculates all of the invoice numbers from top to bottom of the data.



    function makeInvoiceNumbers(){
    var ss=SpreadsheetApp.getActive();
    var sh=ss.getSheetByName('Sheet1');
    var rg=sh.getDataRange();
    var vA=rg.getValues();
    var actRow=sh.getActiveCell().getRow();
    for(var i=1;i<vA.length;i++){
    var cnt=0;
    for(var j=1;j<i;j++){//Working from top to bottom creating invoice numbers
    if(vA[i][0]==vA[j][0]){//I assume that the entrees on the top came before the entrees below them.
    cnt++;
    }
    }
    vA[i][1]=getInvoiceString(vA[i][0].toString(),cnt);
    }
    rg.setValues(vA);
    }
    function getInvoiceString(d,n){
    var dA=d.split('/');
    var yy=dA[2].toString().slice(2);//gets the last two digits of a four digit year
    var mm=padFor2Digits(dA[0]);
    var dd=padFor2Digits(dA[1]);
    var nn=padFor2Digits(String(n));
    return yy+mm+dd+nn;
    }
    function padFor2Digits(s){
    if(s.length<2){
    s='0' + s;
    }
    return s
    }


    Data before running makeInvoiceNumbers()



    enter image description here



    Data after running makeInvoiceNumbers()



    enter image description here



    If I were doing this for myself I would probably use the onEdit trigger and just do it for one line at a time.






    share|improve this answer






























      0














      This isn't exactly what you asked for but it accomplishes the same thing and perhaps it will help you to move further. I formatted column one to be plain text. This calculates all of the invoice numbers from top to bottom of the data.



      function makeInvoiceNumbers(){
      var ss=SpreadsheetApp.getActive();
      var sh=ss.getSheetByName('Sheet1');
      var rg=sh.getDataRange();
      var vA=rg.getValues();
      var actRow=sh.getActiveCell().getRow();
      for(var i=1;i<vA.length;i++){
      var cnt=0;
      for(var j=1;j<i;j++){//Working from top to bottom creating invoice numbers
      if(vA[i][0]==vA[j][0]){//I assume that the entrees on the top came before the entrees below them.
      cnt++;
      }
      }
      vA[i][1]=getInvoiceString(vA[i][0].toString(),cnt);
      }
      rg.setValues(vA);
      }
      function getInvoiceString(d,n){
      var dA=d.split('/');
      var yy=dA[2].toString().slice(2);//gets the last two digits of a four digit year
      var mm=padFor2Digits(dA[0]);
      var dd=padFor2Digits(dA[1]);
      var nn=padFor2Digits(String(n));
      return yy+mm+dd+nn;
      }
      function padFor2Digits(s){
      if(s.length<2){
      s='0' + s;
      }
      return s
      }


      Data before running makeInvoiceNumbers()



      enter image description here



      Data after running makeInvoiceNumbers()



      enter image description here



      If I were doing this for myself I would probably use the onEdit trigger and just do it for one line at a time.






      share|improve this answer




























        0












        0








        0







        This isn't exactly what you asked for but it accomplishes the same thing and perhaps it will help you to move further. I formatted column one to be plain text. This calculates all of the invoice numbers from top to bottom of the data.



        function makeInvoiceNumbers(){
        var ss=SpreadsheetApp.getActive();
        var sh=ss.getSheetByName('Sheet1');
        var rg=sh.getDataRange();
        var vA=rg.getValues();
        var actRow=sh.getActiveCell().getRow();
        for(var i=1;i<vA.length;i++){
        var cnt=0;
        for(var j=1;j<i;j++){//Working from top to bottom creating invoice numbers
        if(vA[i][0]==vA[j][0]){//I assume that the entrees on the top came before the entrees below them.
        cnt++;
        }
        }
        vA[i][1]=getInvoiceString(vA[i][0].toString(),cnt);
        }
        rg.setValues(vA);
        }
        function getInvoiceString(d,n){
        var dA=d.split('/');
        var yy=dA[2].toString().slice(2);//gets the last two digits of a four digit year
        var mm=padFor2Digits(dA[0]);
        var dd=padFor2Digits(dA[1]);
        var nn=padFor2Digits(String(n));
        return yy+mm+dd+nn;
        }
        function padFor2Digits(s){
        if(s.length<2){
        s='0' + s;
        }
        return s
        }


        Data before running makeInvoiceNumbers()



        enter image description here



        Data after running makeInvoiceNumbers()



        enter image description here



        If I were doing this for myself I would probably use the onEdit trigger and just do it for one line at a time.






        share|improve this answer















        This isn't exactly what you asked for but it accomplishes the same thing and perhaps it will help you to move further. I formatted column one to be plain text. This calculates all of the invoice numbers from top to bottom of the data.



        function makeInvoiceNumbers(){
        var ss=SpreadsheetApp.getActive();
        var sh=ss.getSheetByName('Sheet1');
        var rg=sh.getDataRange();
        var vA=rg.getValues();
        var actRow=sh.getActiveCell().getRow();
        for(var i=1;i<vA.length;i++){
        var cnt=0;
        for(var j=1;j<i;j++){//Working from top to bottom creating invoice numbers
        if(vA[i][0]==vA[j][0]){//I assume that the entrees on the top came before the entrees below them.
        cnt++;
        }
        }
        vA[i][1]=getInvoiceString(vA[i][0].toString(),cnt);
        }
        rg.setValues(vA);
        }
        function getInvoiceString(d,n){
        var dA=d.split('/');
        var yy=dA[2].toString().slice(2);//gets the last two digits of a four digit year
        var mm=padFor2Digits(dA[0]);
        var dd=padFor2Digits(dA[1]);
        var nn=padFor2Digits(String(n));
        return yy+mm+dd+nn;
        }
        function padFor2Digits(s){
        if(s.length<2){
        s='0' + s;
        }
        return s
        }


        Data before running makeInvoiceNumbers()



        enter image description here



        Data after running makeInvoiceNumbers()



        enter image description here



        If I were doing this for myself I would probably use the onEdit trigger and just do it for one line at a time.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 2:47

























        answered Nov 14 '18 at 2:39









        CooperCooper

        6,7112726




        6,7112726






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289162%2fuse-google-script-to-count-dates-in-a-column-equal-to-a-specific-cell%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud