►
Description
For source code and input files, check out this link:
https://github.com/sumurthy/officescripts-projects/tree/main/Copy%20Tables%20to%20Master%20Table
A
Hello
in
this
video
I'm
going
to
showcase
how
to
use
office
scripts
to
combine
tables
that
are
located
in
this
excel
file
into
a
single
merge
table.
Now.
This
assumes
that
the
the
tables
that
we're
dealing
with
all
have
the
same
structure
and
they
may
be
located
anywhere
in
the
spreadsheet,
but
as
long
as
they
have
the
same
structure,
which
means
the
same
the
same
columns,
then
we
can
combine
all
of
them
into
a
single
table.
A
A
So
before
I
start
I'll
show
the
different
tables
that
I
have
so,
as
you
can
see,
this
table
has
sort
of
five
columns.
A
There
is
none
here,
and
this
sheet
has
the
same
set
of
columns
but
different
set
of
rows
and
another
one
here,
so
knowing
that
they're
all
located
in
different
places,
so
I
can
just
get
hold
of
all
the
tables
in
this
given
file.
Now,
if
you
want
to
exclude
some
files-
or
if
you
happen
to
have
other
tables
that
don't
match
the
same
criteria,
like
number
of
columns
may
be
different
and
if
you
want
to
exclude
them,
that
is
a
little
bit
more.
A
A
A
So
now
that
I
have
the
new
sheet,
I
can
begin
to
update
the
range.
So
range
is
nothing
but
sort
of
collection
of
cells.
I
can
begin
to
update
the
the
range
with
the
with
the
header
row
and
I
can
pick
up
the
header
row
from
any
of
the
tables
knowing
what
they
are.
I
can
even
hard
code
it,
but
let's
be
more
dynamic.
A
A
And
new
sheet
offers
add
table
api,
so
add
table
api
takes
in
two
parameters.
One
is
the
the
starting
address,
or
rather
the
the
address
where
you
want
to
create
the
table
and
a
boolean
property
that
has
that
indicates
whether
the
data
that
you're
creating
table
on
top
of
whether
it
has
the
header
or
not.
A
So
I'm
not
ready
yet
because
I
don't
have
the
the
new
sheet
that
I've
created,
I
don't
have
any
data
to
begin
overlaying
the
table
on
top
of.
So
I'm
going
to
comment
this
for
now
and
think
about
creating
you
know
when
I
create
a
new
sheet.
My
goal
is
to
let's
say
you
know,
take
the
header
and
then
and
then
update
the
new
sheet
with
that
right.
A
A
And
you
should
see
those
five
different
columns
in
a
two-dimensional
array
right,
so
those
are
all
the
table.
Column
values
I
expect
so
now.
What
I
want
to
do
is
take
this
and
update
on
top
of
let's
say,
beginning
at
a1
on
a
new
sheet,
then
I'm
going
to
create
so
for
that
I
will
write,
say
a
function
that
takes
in
two
arguments.
A
One
is
the
with
the
worksheet
where
I
want
the
data
to
be
updated
and
then
and
then
I'll
I'll
accept
the
actual
values
that
I
want
to
update
I'll
call
it
data.
A
It
call
it
update
range.
Let
me
go
back
and
get
the
text
rather
than.
A
Well,
if
you
know
that
this
has
five
columns,
you
know
I
can
say
a1
through
e1,
but
let's
assume
you
know,
the
structure
of
the
table
could
be.
You
know
dynamic
in
nature.
You
know
tomorrow,
I
might
want
to
run
against
another
excel
file
that
has
different
number
of
columns,
so
I
want
to
make
it
a
little
bit
more
dynamic
so
for
that
I'll
compute,
the
target
range
so
I'll,
say,
target
range.
A
So
I'll
get
the
sheets
I'll
begin
with
a
starting
point
which
is
a1
so
which
is
the
top
left
corner
and
then
I'll
get
resize
range
so
which
means
from
there
give
me
another
range
and
that
takes
two
parameters.
One
is
the
number
of
rows
now.
The
number
of
rows
is,
we
know
it's.
A
A
A
Now
I'm
gonna
subtract
one
from
each
given
that
I'm
already
you
know,
I
need
the
relative
size,
so
I'm
going
to
get
the
so
I'm
going
to
subtract
by
1..
So
now
I
just
simply
display
the
new
range
address
just
to
make
sure
I'm
on
the
right
path
here
right.
So
all
this
is
doing
is
extracting
the
header
row
of
the
first
table
and
then
it's
calling
this.
Let
me
call
this.
A
Pass
in
the
new
sheet
I'll
pass
in
the
header
values,
so
that's
all
I'm
gonna
do
one
additional
thing
I'm
gonna
do
is
because
I'm
creating
a
new
worksheet.
I
will
first
remove
it
just
so.
A
I
can
rerun
this
again
and
again
without
having
to
run
into
duplication
issues.
So
for
that
I'll
get
worksheet
with
the
same
name.
I
think
I
called
it
combined
and
then
delete
it
if
it
exists
all
right.
So
if
I
run
this,
I
should
see
well.
I
should
see
the
address
of
where
the
all
right,
so
it's
gonna
compute,
so
it's
computing
it
correctly
so
which
is
on
this
worksheet
a1
through
e1,
is
where
I
want
to
update.
A
So
that
looks
good
and
then,
instead
of
displaying
I'm
gonna
just
use
that
range
to
set
values,
and
I
will
give
it
the
data
right
so
now,
if
you
are
on
not
only,
it
should
create
a
combined
worksheet,
but
it
should
also
update
that
with
the
actual
column
values.
So
that
looks
good.
A
So
that's
all
I
need
from
this
function.
I
think
it's
doing
what
it
needs
to
do
and
then
I'm
gonna
now
create
this
table.
So
maybe,
instead
of
returning
nothing,
I
will
create
I'll
return.
The
actual
actual
range
that
I
updated
this
with.
A
A
A
A
And
on
the
combined
table,
I'm
simply
going
to
add
rows.
Now.
Add
rows
accepts
two
parameters.
One
is
the
the
index,
so
I
want
it
at
the
end
and
then
I
will
say
you
know
the
values.
Are
the
data
values?
A
A
A
new
table
with
all
the
rows
that
I
need
so
let's
confirm
so
I
have
e107
through
114
and
then
115
to
119
and
then
120
through
128.
So
I
think
I
should
have
all
of
those
here.
A
Okay,
so
that
kind
of
completes
the
initial
goal
of
combining
all
the
the
table
into
a
single
table.
Now,
what
if
you
wanted
to?
You
know,
eliminate
some
table
from
this
list,
so
so,
let's
assume
that
you
know
how,
when
one
of
these
sheets,
I
have
a
table,
let's
just
insert
something.
A
You
know
I
don't
want
to
pick
up
this
table.
Obviously
this
is
different
from
this
structure
that
I
created.
So
there
are
kind
of
couple
of
ways
to
do
this.
A
One
is
for
each
of
the
table
like
if
you
happen
to
know
the
table
name
for
in,
for
instance,
like
this
table
has
a
name
and
if
you
know
the
exact
tables
that
you
want
to
copy,
if
I
want
to
collect-
and
one
thing
you
could
do
is
instead
of
getting
all
the
tables
you
could
just
get
only
the
tables
that
you're
interested
in
the
other
thing
you
could
do
is,
if
you
know
the
worksheets,
where
you
have
your
target
tables,
then
you
can
just
say:
hey
pick
up
tables
only
from
these
set
of
sheets
and
then
remove
the
rest,
so
maybe
that
that's
probably
an
easier
easier
place
to
do
that.
A
A
Sheet
names
from
where
I
want
to
pick
up
so
I'll
call
it
sheet1
and
sheet2
and
then
sheet
3.
So
it
will
not
pick
up
the
new
table
that
I
created
here.
So
it'll
only
pick
up
from
those
three
different
sheets,
so
sheets
themselves
have
get
tables
api.
So,
for
instance,
here
there
is
the
workbook
get
tables,
but
she
will
also
contain
the
workbook.
You
know
get
tables.
A
A
A
Call
it
copy,
select
tables
so
so
I'll
have
to
work
this
logic
a
little
bit
which
is
instead
of
getting
all
the
tables
here.
I
only
want
to
get
tables
for
each
of
these
sheets,
so
this
entire
logic
will
need
to
go
underneath
another
loop,
I'll
call
it
sheet
names.
A
A
So
for
each
of
the
sheet
names
I'm
going
to
get
the
actual
sheet
and
then
I'll
call
the
git
tables
on
it.
So
from
then
on
it
should
work
pretty
much
the
same,
except
that
I
don't
want
to
create
the
the
target
table
more
than
once
right.
So
for
that
purpose,
I
will
extract
this
piece
out
or
maybe
do
that
just
once.
A
A
A
So
what
what
this
is
doing
is
if
there
is
no.
If
there
is
no
table,
the
target
table
has
not
been
created.
A
It'll
pick
up
the
first
table
of
the
target
sheet
and
it
could
be
any
sheet
doesn't
matter
because
they're
all
the
same
and
then
it'll
you
know
create
you
know,
get
the
target
range.
It'll
call
the
same.
Update
range
function,
receive
the
target
range
and
create
the
the
table,
and
that
will
mark
the
table
created
as
true.
So
that
way
it
won't
pick
up.
A
It
won't
go
into
this
logic
again,
so
it's
going
to
just
create
the
table
once
and
then
for
each
of
the
sheet
it's
going
to
get
in
all
the
sheet,
all
the
tables.
Well,
in
this
case,
I
only
happen
to
have
one,
but
if
you
had
multiple
tables
in
the
same
sheet,
it'll
work
the
same
and
then
for
each
of
the
table.
It
is
going
to,
you
know,
append
the
values
in
terms
of
address.
A
A
Okay,
so
if
I
go
back
here
now,
you
see
that
it
is
working
correctly
now
to
test
to
see
if
the
other
scenario
that
I
talked
about,
where
imagine,
had
different
tables
or
multiple
tables
in
the
same
worksheet.
So
let's
just
make
it
e130
just
to
test.
A
Okay,
all
right,
so
you
see
that
even
if
a
sheet
has
multiple
tables,
it'll
pick
up
and
update
it
so
again,
so
this
is
a
bit
more
advanced
scenario,
but
you're
likely
gonna
end
up
using
the
first
scenario,
which
is
you
know,
the
structure
of
the
tables,
and
that
is
all
the
set
of
tables
you
have
and
and
then
the
previous
script
that
I
wrote,
which
is
a
coffee
copy
tables,
just
combines
them
all
into
a
single
table.
A
So
hope
this
was
useful,
find
the
source
code
for
each
of
the
samples
in
the
description
link
and
if
you
have
any
questions
or
comments,
please
you
know
leave
a
comment
at
the
bottom.
Thank
you.