Make a Button to Increment / Decrement a Value

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ม.ค. 2025

ความคิดเห็น • 111

  • @Felixitously
    @Felixitously 7 หลายเดือนก่อน +6

    Hugely helpful, had no idea it'd be such a pain to search up a solution for what I thought would be a common need, but your explanation was succinct and simple! Much appreciated (:

  • @jcravenclaw
    @jcravenclaw ปีที่แล้ว +2

    Thank you so so much. I had no idea this was possible

    • @SpencerFarris
      @SpencerFarris  หลายเดือนก่อน

      Apps Script allows abilities some consider... unnatural

  • @tymccabe
    @tymccabe 10 หลายเดือนก่อน +1

    This is great, thanks! Any ideas to make it work on the app version of the spreadsheet? It works perfectly on my laptop, but when i open the sheet in the app touching the buttons does nothing.

    • @SpencerFarris
      @SpencerFarris  10 หลายเดือนก่อน

      You have to use the checkbox with onEdit(). Buttons do not work on mobile

  • @bliskisusreti
    @bliskisusreti ปีที่แล้ว +1

    Solved my big problem! Huge thanks!

  • @BK-iy4jj
    @BK-iy4jj 7 หลายเดือนก่อน +3

    here I thought this is a google sheet tutorial, didn't expect it to be a coding tutorial

    • @SpencerFarris
      @SpencerFarris  7 หลายเดือนก่อน +1

      Hope you're not disappointed

    • @AdmiralArchon
      @AdmiralArchon 6 หลายเดือนก่อน +1

      unfortunately the limitations in google sheets makes light coding necessary. Complain to Google for not having user friendly features. Its BS to have to go through all this, but I appreciate the tutorial

    • @SpencerFarris
      @SpencerFarris  6 หลายเดือนก่อน

      @@AdmiralArchon not sure what the issue is. AFAIK no spreadsheet program can do things like this without coding. This is very much stretching what a spreadsheet does. The general rule is that a cell can be populated by manual edit OR formula and that formulas SHOW things, they do not DO things.
      So yeah, light coding. Not a Google issue, a spreadsheet fact.

    • @AdmiralArchon
      @AdmiralArchon 6 หลายเดือนก่อน

      @@SpencerFarris I was intending to respond to the guy digging at your video if how to do it. You must use "coding" to get Google sheets to cooperate.
      Excel in fact does an excellent job at adding these features without having to use code.
      In addition, the time it takes for the script to run in Google sheets is crazy for what it is.
      I ended up just switching to Excel because I need to tick items quickly up or down. Not wait 2-3 seconds for every tick.
      Again great job explaining what needs to be done, but Google sheets does this very poorly compared to excel.

    • @binayashrestha63
      @binayashrestha63 หลายเดือนก่อน

      Please make a video to do same thing in appsheet

  • @johnfernandez9170
    @johnfernandez9170 ปีที่แล้ว +3

    Very helpful! Could you make a function to increment the value in a highlighted cell as well?

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      WDYM?

    • @marco.castiglia
      @marco.castiglia ปีที่แล้ว

      @@SpencerFarris I suppose he means a general increment button, which increments the value of a selected cell

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      @@marco.castiglia Perhaps, which should be simple because you can access the Active Cell

    • @marco.castiglia
      @marco.castiglia ปีที่แล้ว

      @@SpencerFarris I'm not really capable of doing so imho. A general management button might be quite a thing, you should give it a try

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      @@marco.castiglia hm?

  • @JairoChristopherPeraltaAyala
    @JairoChristopherPeraltaAyala 25 วันที่ผ่านมา

    Great!!
    Is it possible to send the button to a website created by Google sites?

    • @SpencerFarris
      @SpencerFarris  18 วันที่ผ่านมา

      Yes. Lookup "Google Apps Script open url"

  • @MystixBlack
    @MystixBlack 3 หลายเดือนก่อน +1

    this is kinda exactly what i needed but missing the important part, i want those 2 buttons but they shoudnt only work on the A2 Box, it should increase or decrease the box i highlighted/clicked before. how would i do that?

    • @MystixBlack
      @MystixBlack 3 หลายเดือนก่อน

      ah found it^^ its sheet.getActiveCell() works perfekt^^ thanks for the vid

    • @SpencerFarris
      @SpencerFarris  3 หลายเดือนก่อน

      You would use .getActiveRange() as the thing to increment/decrement

  • @phoenixarzuljin2578
    @phoenixarzuljin2578 หลายเดือนก่อน

    i want to increment/decrement F by E on 40 different row. i've tried some things but i cant get it to work, i'm pretty sure i can do with less coding than 2 button for each row. i dont mind if every row increment at the same time if i can reset all E row to 0.
    to make it more concrete, i want to have an initiative sheet for dnd, and on this sheet i want a column with damage/healing received as value (E), a second column beeing the cumulative damage (F), after its easy i have a total hp column (G) and a dead or alive/remaining hp (H) which use a if/else function.
    i can get the script to work on a button, for each row, but since i have to draw the button for each and its making different size button my ocd are cranking it up XD (if you could help thanks)

    • @SpencerFarris
      @SpencerFarris  18 วันที่ผ่านมา

      Use the checkbox method in my other video

  • @gtkaparos
    @gtkaparos ปีที่แล้ว +1

    Great video 1 question. What if you have a row of 10 items you wish to increment/decrement, would you need to make a script for each row?
    i.e. I have row f3-f12, they do not get incremented at once but individually as needed. I'm guessing I will need to copy and paste the script 12 items and adjust with each item to get the scripts to work for each indivdual cell. Any way to program the script to only work on the active selected cell instead?

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว +2

      That is possible using Spreadsheet.getActiveCell(). But you can also make it work with checkboxes that have a position they know.
      th-cam.com/video/RfAyBORczHc/w-d-xo.html

    • @gtkaparos
      @gtkaparos ปีที่แล้ว +2

      @@SpencerFarris Thank you for the prompt reply. I will check out this video as well and see if I can make it work. This is part of a vanity project on my end. I'm no script kitty, more of a Jack of all Trades

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว +1

      @@gtkaparosGood luck!

    • @josecarlomendoza1231
      @josecarlomendoza1231 ปีที่แล้ว +1

      Good day good sir@@SpencerFarris . My concern is like wise, i would like to have increment and decrement button on 20 cells vertically, is there a way to short cut it ? like do i have to copy and paste it then change each designated cells 20times? i would like button not check boxes sir

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      @@josecarlomendoza1231 You will have to make indiovidual buttons with their own script

  • @Dunleap
    @Dunleap 24 วันที่ผ่านมา

    what if I want to do that for column with over 20 positions. Do I have to write 20 scripts for each cell or can I somehow copy it?

    • @SpencerFarris
      @SpencerFarris  18 วันที่ผ่านมา

      With buttons, yes. But I would absolutely use the checkbox option in my other video

    • @Dunleap
      @Dunleap 18 วันที่ผ่านมา

      @ can you link me to it?

    • @SpencerFarris
      @SpencerFarris  18 วันที่ผ่านมา +1

      @@Dunleap th-cam.com/video/RfAyBORczHc/w-d-xo.html

  • @martinlopez3650
    @martinlopez3650 ปีที่แล้ว

    Brother you saved my day 🎉

  • @frankensteinsmonsieur
    @frankensteinsmonsieur 8 หลายเดือนก่อน

    Hi, would it be possible to increase/decrease the cell by the value in a separate cell instead of by increments of one? Thank you!

    • @SpencerFarris
      @SpencerFarris  8 หลายเดือนก่อน

      For sure! Let's assume you want to increase/decrease by the value in D2
      function incrementA2() {
      const sheet = SpreadsheetApp.getActiveSheet();
      const range = sheet.getRange("A2");
      let changeVal = sheet.getRange("D2").getValue();
      range.setValue(range.getValue()+changeVal);
      }

  • @beppef5797
    @beppef5797 ปีที่แล้ว +1

    many thanks!

  • @ArmadusMalaysia
    @ArmadusMalaysia 7 หลายเดือนก่อน +1

    nice! Just what I was looking for, but I noticed how much it lags. So it's not going to be as fast as choosing numbers from a menu.

    • @SpencerFarris
      @SpencerFarris  หลายเดือนก่อน

      So many variables. RAM, browser usage, Internet connection...

  • @dandanai1713
    @dandanai1713 ปีที่แล้ว

    Great video, thank.
    I have one key problem, I'd like to use the sheet on my android phone, and there the buttons are not active. Looks like the script isn't running on them.
    Can this be fixed?

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      onEdit scripts are the only type that work on mobile

    • @dandanai1713
      @dandanai1713 ปีที่แล้ว

      What's onEdit? How do i go about getting this? Thanks!!!

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      @@dandanai1713 th-cam.com/video/RfAyBORczHc/w-d-xo.html

  • @HoshiHikari
    @HoshiHikari 3 หลายเดือนก่อน

    How do I run this in the copy I made? It's telling me I need Authorization and to verify some app, but I don't know what app.

    • @SpencerFarris
      @SpencerFarris  3 หลายเดือนก่อน

      It's asking for authorization / verification of the script. The script runs as you, or the triggering user, so it's just asking you to say "yes, I authorize you to run as me"

  • @ChaseKelleh
    @ChaseKelleh 9 หลายเดือนก่อน

    Shot in the dark here, how would you make a script to reset a value to a value defined within another cell?
    for example, L9:L13 go down with their own individual buttons. Id like to press one button and refresh the values in L9:L13 to values held in M9:M13

    • @SpencerFarris
      @SpencerFarris  9 หลายเดือนก่อน

      ss.getRange("L9:L13").setValues(ss.getRange("M9:M13").getValues())

  • @LuckyImp
    @LuckyImp ปีที่แล้ว

    the code is not working on a new sheet when its assigned the script it says it cannot be found, But works fine in the copied sheet from your description?
    What am i missing...?
    They both have the same script name.

    • @SpencerFarris
      @SpencerFarris  หลายเดือนก่อน

      Sorry it's been SO LONG. Are you still having issues?

  • @adamsewell8628
    @adamsewell8628 ปีที่แล้ว

    Nice and simple thanks. I want to add multiple counters to a sheet that will refer to different target cells, is there an effective way to do this in one code? I have tried adding a target variable in increment(targetCell), but it throughs and error "Exception: Argument cannot be null: a1Notation" assume because it it empty initially, thank you

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว +1

      You cannot pass arguments to a function triggered by a button.

    • @lyn4739
      @lyn4739 11 หลายเดือนก่อน

      @@SpencerFarris Does this also mean it's impossible to do the following:
      Increment a given cell by the amount in another cell when pressing a button.
      Thank you!
      I would love to be able to make it a "general" button that works with whatever you give it.

    • @SpencerFarris
      @SpencerFarris  11 หลายเดือนก่อน +1

      ​@@lyn4739 As long as the cell reference is static, that's fine. So if you always want to increment A2 by whatever is in N2, that's fine.

    • @lyn4739
      @lyn4739 11 หลายเดือนก่อน

      @@SpencerFarris awesome, thank you!
      The increment amount will always be in a specific cell.
      I will mess around with it :)

  • @paulzord
    @paulzord 4 หลายเดือนก่อน

    Love your work mate.
    Ken oath

  • @Mafero1207
    @Mafero1207 4 หลายเดือนก่อน

    thanks a lot, this was very useful; I've tried replicate this code to perform other action but replacing 1 to 0,01 (to use as percent) and I've get an error message, so… it's possible to use 0,01?

    • @SpencerFarris
      @SpencerFarris  3 หลายเดือนก่อน +1

      What error did you get?
      You may need to check the locale settings OR use .01 instead of ,01

  • @vinicius2336SP
    @vinicius2336SP 7 หลายเดือนก่อน

    AMAZING! Thank you so much!

  • @papawhowho
    @papawhowho 4 หลายเดือนก่อน

    Can I use this script to run when a bar code is scanned

    • @SpencerFarris
      @SpencerFarris  หลายเดือนก่อน

      I've never dealt with bar code systems; IDK

  • @Rubijuana
    @Rubijuana ปีที่แล้ว

    Do you know how to prevent it from opening the Picture Editor by clicking the button multiple times too fast? And is it possible to hide the "running script" message?

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      Neither of those are possible

    • @Rubijuana
      @Rubijuana ปีที่แล้ว

      @@SpencerFarris all good, i did it 😂👌

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      @@Rubijuana how???

    • @Rubijuana
      @Rubijuana ปีที่แล้ว

      @@SpencerFarris you can use a picture you upload yourself, instead of using the drawing function of sheets...you cant double klick that one^^ but the "running script" is more complicated...i am still trying to figure out, how to write an app to avoid it

  • @feareddevil
    @feareddevil ปีที่แล้ว

    THANK YOU SO MUCH MAN

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      YOU'RE SO WELCOME

    • @feareddevil
      @feareddevil ปีที่แล้ว

      lol also how would I add text before the number counter?@@SpencerFarris

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      @@feareddevil Like what, exactly?

    • @feareddevil
      @feareddevil ปีที่แล้ว

      @@SpencerFarris a example would be like 5 rubies collected

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว +1

      @@feareddevil Oh... that's honestly quite a bit more difficult. How about you make a test sheet and post it here for us to look at?

  • @Nico..
    @Nico.. 7 หลายเดือนก่อน

    is there a way to make multiple buttons like this? e. g. i want to have a "+" and a "-" button each for 2 players, how could i make this work?

    • @SpencerFarris
      @SpencerFarris  7 หลายเดือนก่อน

      Sure. Just name them differently

  • @DandeFX
    @DandeFX ปีที่แล้ว

    Hello, first of all thanks for the vid.
    I'm new to coding and I would like to increase the value of 2 fields with +1 instead of only one field.
    I tried different things but was unable to get it running. Could you help me?

    • @DandeFX
      @DandeFX ปีที่แล้ว

      @spencerfarris

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว +1

      @@DandeFX Here is for 2 different cells. Add as many as you would like in the array.
      function incrementA2() {
      const sheet = SpreadsheetApp.getActiveSheet();
      const ranges = ["A2","E2"];
      for (let i in ranges)
      sheet.getRange(ranges[i]).setValue(range.getValue()+1);
      }

    • @DandeFX
      @DandeFX ปีที่แล้ว +1

      @@SpencerFarris thanks a lot

  • @Employee1-rl3wd
    @Employee1-rl3wd 11 หลายเดือนก่อน

    How can you make the buttons works in smartphone or touchscreen devices? thank you for your reply

    • @SpencerFarris
      @SpencerFarris  11 หลายเดือนก่อน +1

      Buttons do not work on mobile. Use checkboxes and onEdit scripts

    • @Employee1-rl3wd
      @Employee1-rl3wd 11 หลายเดือนก่อน

      @@SpencerFarris Thank you for your response... can you show how to do the script for that?

    • @SpencerFarris
      @SpencerFarris  11 หลายเดือนก่อน

      @@Employee1-rl3wd On the other video you found

  • @Holden2341
    @Holden2341 ปีที่แล้ว

    Good video. I have a question. How do I increase a cell formula number daily? For example, let's say I have a formula =SUM(B1:B31), and the next day I want it to be =SUM(B2:B31), and the day after that I want the B2 to be B3 and so on and so forth. Thanks!

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      This feels like an X/Y problem. I imagine there are dates or some other sequence of numbers dictating which rows should be summed?

    • @Holden2341
      @Holden2341 ปีที่แล้ว

      @@SpencerFarris Thanks for the quick reply. Yes, it's dates. I'm basically looking for a rolling 7 day total. So let's say for today I want =SUM(B23:B29)(cells B30 and B31 are empty as of today), I'm mostly just concerned with being able to increase the first cell # by 1 each day. OR, any method to achieve the desired outdome. Thanks!

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว +1

      @@Holden2341This will be much easier with SUMIFS() comparing dates

  • @durcastraushun288
    @durcastraushun288 5 หลายเดือนก่อน

    so, it only works for 1 cell, if I have 100 cells, i have to do drawings for each one and enter the code for each one

    • @SpencerFarris
      @SpencerFarris  5 หลายเดือนก่อน

      Use checkboxes :)
      th-cam.com/video/RfAyBORczHc/w-d-xo.html

  • @TheRenegadeLife
    @TheRenegadeLife ปีที่แล้ว

    This was extremely helpful! Thank you for the easy to follow video. I'd like to add one more thing when I click the button. To add the current date in the cell next to it. Can you help me with that code?

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      That's impossible, because the button doesn't have a definite position on the sheet that the script can access. Use checkboxes instead
      th-cam.com/video/RfAyBORczHc/w-d-xo.html

  • @raphaeljackson7
    @raphaeljackson7 ปีที่แล้ว

    Can this be applied to multiple cells with one click? And can it be made to stop at zero?

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      Sure, just loop through the ranges and check for 0 at the beginning of the loop

    • @wesjgoldberg
      @wesjgoldberg ปีที่แล้ว

      @@SpencerFarris for the making it stop at zero, is there any chance you could dumb that explanation down for someone who has zero knowledge whatsoever and just copied & pasted your script? 😅 it'd also be great to know how to cap the counter based on the value of cell, if that's possible. Again, I know nothing about this stuff

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว +1

      @@wesjgoldberg function decrementA2(){
      const sheet = SpreadsheetApp.getActiveSheet();
      const range = sheet.getRange("A2");
      range.setValue(Math.max(range.getValue()-1,0));
      }
      function incrementA2() {
      const sheet = SpreadsheetApp.getActiveSheet();
      const range = sheet.getRange("A2");
      const maxVal = sheet.getRange("B2").getValue();
      range.setValue(Math.min(range.getValue()+1,maxVal));
      }

  • @owkttwilane5928
    @owkttwilane5928 8 หลายเดือนก่อน

    Hi, can im Decrement many cells with one button?

    • @SpencerFarris
      @SpencerFarris  8 หลายเดือนก่อน

      Sure. I would just get all the values in the range, decrement everything, then put them back. Something like this if you want all of A2:A
      function decrementAll(){
      const sheet = SpreadsheetApp.getActiveSheet();
      const range = sheet.getRange("A2:A10");
      let values = range.getValues();
      for (let i = 0; i < values.length; i++)
      values[i][0]--;
      range.setValues(values);
      }

  • @dodhethompson4841
    @dodhethompson4841 วันที่ผ่านมา

    Not sure why but this isn't doing anything
    This coming from someone that copied what you did part by part

  • @revg9742
    @revg9742 ปีที่แล้ว

    when i use menu EXTENSION > Apps Script it error "Bad Request, ERROR 400"

    • @SpencerFarris
      @SpencerFarris  ปีที่แล้ว

      I really can't help with that issue. Try googling "apps script error 400" and see if anything helps

  • @_Anonymous_Participant
    @_Anonymous_Participant ปีที่แล้ว

    I got tricked by Discord message sound in your video.

  • @ldxyz-s1e
    @ldxyz-s1e ปีที่แล้ว +1

    It is so slow, so unusable!

    • @SpencerFarris
      @SpencerFarris  หลายเดือนก่อน

      Lots of factors go into the speed. I do suggest using checkboxes, instead