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
 All Forums
 Origin Forum
 Origin Forum
 Writing a script to get the sum of a column

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
jolz Posted - 12/09/2015 : 8:19:30 PM
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:
3   L A T E S T    R E P L I E S    (Newest First)
JacquelineHe Posted - 12/10/2015 : 9:45:43 PM
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
jolz Posted - 12/10/2015 : 10:20:42 AM
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?
JacquelineHe Posted - 12/10/2015 : 04:41:07 AM
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

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000