Relatively Easy Script... I think...?

59 views
Skip to first unread message

Nathan McAnsh

unread,
Sep 20, 2021, 3:53:13 PM9/20/21
to Google Apps Script Community
Hi Everyone!!

I am a complete novice to Google Sheets and looking for some help on what I THINK will be a pretty easy script? Apologies if this ends up posted twice. Tried posting it once about 20 minutes ago but doesn't seem to have worked so trying again!  :)

On my Google Sheet, on the Duplicate Registrations tab, I have a list of Invoice Numbers between I7 and I152. These cells can only have three possible values - a 13-digit number, a hyphen (-), or it can be blank. These cells are being populated by a VLOOKUP to my calculations tab, which is in turn pulling its data from half a dozen other tabs. 

All I need the script to do is look at each cell between I7 and I152. If the value is a number, copy/paste value that number into Column B on the same Row. If it is a Hyphen or if it is blank, ignore it and move on to the next.

I also have a question about how to trigger the script. Does Google Sheets allow you to add a button, or does it have to be a CTRL-ALT-SHIFT-# shortcut?

Thank you in advance!! I really appreciate your help!


Nathan McAnsh

unread,
Sep 22, 2021, 12:16:23 PM9/22/21
to Google Apps Script Community
Hi Everyone!!

If it helps, this is how I would do it in MS Excel. I just don't don't know how to translate that to Google Sheets, and I don't know how to trigger it...

    Dim r As Integer
    For r = 7 To 152
    If Cells(r, "I") > 0 Then
        If Cells(r, "I") <> "-" Then
            Range("I" & r).Select
              Selection.Copy
              Range("B" & r).Select
              Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                  :=False, Transpose:=False
        End If
    End If
    Next

Thanks again!!

Nathan McAnsh

unread,
Sep 22, 2021, 10:56:55 PM9/22/21
to Google Apps Script Community
Hey Everyone Again!

I was able to get this figured out!  :)  Posting the solution, just in case someone comes across this at some point down the road looking the same thing:

  var sheet = SpreadsheetApp.getActive()
  var range = sheet.getRange("I7:I152");
  var values = range.getValues();

  Logger.log(values);

  row_index = 7; 

  values.forEach(function(i_column_value) {

    Logger.log(row_index, i_column_value);

    if (i_column_value != "") {
      sheet.getRange('B' + row_index).setValue(i_column_value);
    }
    row_index++;

  });

Kim Nilsson

unread,
Sep 23, 2021, 6:40:26 AM9/23/21
to Google Apps Script Community
I may not be good enough to see it, but, where's the condition that takes care of rows with hyphens?
Reply all
Reply to author
Forward
0 new messages