The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum
 Origin Forum
 Writing a script to get the sum of a column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

jolz

3 Posts

Posted - 12/09/2015 :  8:19:30 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi I have multiple columns with Z data. I want to add all the values in a column and then put that value in a new column in a separate worksheet. So basically I want to end up with a single column of values the number of rows equal to the number of columns I originally had. How do I write a script to do this? Please help.Origin Ver. and Service Release (Select Help-->About Origin):
Operating System:

JacquelineHe

287 Posts

Posted - 12/10/2015 :  04:41:07 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

If I do not misunderstand your mean, there are multiple columns in a worksheet, and you want to calculate sum for each column. Then put the result to new worksheet column, right?

You can use the Statistics tool to get it. Also you can use loop Labtalk script to get that.
I assume you need to calculate the sum value from Column 1 to 10 in the Book1 sheet1. Open the Script Window, enter the following script, highlight the script and press Enter key.


newsheet;  

//loop for column 1 to 10 to calculate the sum value
for (ii=1; ii<=10; ii++)
{
range aa=[book1]sheet1!col($(ii)); 
sum(aa);
double bb=sum.total;

col(A)[$(ii)]=bb; //put the result to the newsheet Column A
}


Please refer to help page for the loop and sum Labtalk script:
http://www.originlab.com/doc/LabTalk/ref/For-cmd
http://www.originlab.com/doc/LabTalk/ref/Sum-obj


Thanks
Jacqueline
OriginLab

Edited by - JacquelineHe on 12/10/2015 04:42:29 AM
Go to Top of Page

jolz

3 Posts

Posted - 12/10/2015 :  10:20:42 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks! This works but what I really meant to do was put it in a new Book not sheet. Also what if I had multiple books with the same kind of Z data to sum all the columns and then put them all into the same new book with the same sheet but different columns?
Go to Top of Page

JacquelineHe

287 Posts

Posted - 12/10/2015 :  9:45:43 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

You can new a book to output the sum result:

for example:

//New a workbook, and use "SumBook" as Long Name and short name
newbook name:="SumBook" option:=lsname;
//set Column1 longname in the new book
wks.col1.lname$= SumBook1; 

for (ii=1; ii<=10; ii++)
{
range aa=[book1]sheet1!col($(ii)); 
sum(aa);
double bb=sum.total;

[SumBook]sheet1!Cell($(ii),1)=bb; //put the result to the newsheet Column 1
}



If the multiple books with the similar name, for example: Book1, Book2, Book3... and the number columns in a sheet are the same.
You can try to set the nested loop script:
I assume the columns your need to be calculate are in "Sheet1", and there is 3 books, and each book with 10 columns.


newbook name:="SumBook" option:=lsname;
wks.ncols=3; // set the column number in the new book

for (jj=1; jj<=3; jj++)
{
wks.col$(jj).lname$= SumBook$(jj);

for (ii=1; ii<=10; ii++)
{
window -a SumBook;
range aa=[book$(jj)]sheet1!col($(ii)); 
sum(aa);
double bb=sum.total;

[SumBook]sheet1!Cell($(ii),$(jj))=bb; //put the result to the newsheet Column 1
}

}



But if the name and the structure are different of the workbooks, I think that is not fit for this nested loop.

And you can study the Labtalk script in our online help page:
http://www.originlab.com/doc/LabTalk

Thanks
Jacqueline
OriginLab

Edited by - JacquelineHe on 12/10/2015 9:49:11 PM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000