I need a script that increments the value of a cell by 1.

486 views
Skip to first unread message

Michael

unread,
Sep 5, 2023, 3:23:59 AM9/5/23
to Google Apps Script Community
I'm making a macro that does several things, but one thing I want it to do is to increment the value in a specific cell by 1.  For the life of me, I can get it to do everything else I want (including selecting the cell in question), but I can't figure out how to get it to add +1 to that cell.

Brett Grear

unread,
Sep 5, 2023, 7:12:27 AM9/5/23
to Google Apps Script Community
Can you share the current script you have?

Martin Molloy

unread,
Sep 5, 2023, 10:52:23 AM9/5/23
to google-apps-sc...@googlegroups.com
try this

function incrementCellValue(){
let spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
let targetSheet = spreadsheet.getSheetByName('Sheet1')
let targetCell = targetSheet.getRange('A1')
let currValue = targetCell.getValue()
if(isNaN(currValue)){
  console.log('Cell does not contain a number')
  return
}
let newValue = currValue+1
targetCell.setValue(newValue)

}

Martin 


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/570a7d86-359a-4290-bc1c-330ae28509b9n%40googlegroups.com.

Michael

unread,
Sep 5, 2023, 2:43:35 PM9/5/23
to Google Apps Script Community
Thanks, that works!
Reply all
Reply to author
Forward
0 new messages