-1

this script changes E1 to 36 and then clears D1. i.e it add two cells and then clears the other cell. D1 E1 10 26

 var ss = SpreadsheetApp.getActiveSheet(); 
 var num1 = ss.getRange("D1").getValue(); 
 var num2 = ss.getRange("E1").getValue();
 ss.getRange("E1").setValue(num1+num2); 
 ss.getRange("D1").clear();

As i have copied code from Google spreadsheet script to add two cells together. i want to aplly above example to all cells in columns i.e entire column D and column E.

Community
  • 1
  • 1
vikas bhosale
  • 11
  • 1
  • 3

2 Answers2

3

This is an answer:

function addColumns(){
  var ss = SpreadsheetApp.getActiveSheet();
  var len = ss.getLastRow();
  for(var i = 1 ; i < len +1  ; i++){
    var num1 = ss.getRange("D"+i).getValue(); 
    var num2 = ss.getRange("E"+i).getValue();
    ss.getRange("E"+i).setValue(num1+num2); 
    ss.getRange("D"+i).clear(); 
  }
}

It is not at all efficient ... so you will find it strangely slow. But it works.
I agree with @eddyparkinson that it is good to learn google apps script (which is pretty much the same as javascript) because you are likely to get a lot out of it. I had to start learning it from scratch about 1 year ago, mainly by looking at this particular forum for google apps script and then, when I got stuck, referencing the huge quantity of javascript general help you can find on the internet just by doing a search for the term in hand.

EDIT To make it apparent, I have shared this script within a spreadsheet (click here). In that spreadsheet, there is a custom menu called "Script Center Menu>Add column D to E

This will then add all the values in column D Sheet1 to those in column E Sheet 1, displaying them in column E and deleting the original value in column D. (If you wish to experiment, you can prepare the columns' values manually, simply by typing your required values into columns D and E - it may be easier to first clear current values in columns D and E. An alternative way to set yourself up the values for a demonstration is to use the second menu item Script Center Menu>Copy Columns which will simply copy the existing values in Sheet2 over to Sheet1).

David Tew
  • 1,441
  • 1
  • 10
  • 12
  • not able run script.This is error given by google script: TypeError: Cannot call method "getLastRow" of null. – vikas bhosale Aug 18 '13 at 10:30
  • It seems the error that you are reporting that the active sheet cannot be found by the script. I cannot say why this is ... have you modified the script? Anyhow, to show you this is action I have put the script in the following spreadsheet [note, you can use the custom menu to add some values to columns D and E] https://docs.google.com/spreadsheet/ccc?key=0AvhOXv5OGFq6dDBpaG51d253RnQyS1pQb1VVcURXLWc&usp=sharing – David Tew Aug 18 '13 at 11:08
  • i want values in column D to cleared after adding . i dont want value 1 to add. pls refer example in http://stackoverflow.com/questions/16200931/google-spreadsheet-script-to-add-two-cells-together. here script is for only cell and i want for entire column. – vikas bhosale Aug 18 '13 at 19:03
  • @vikasbhosale I have edited my original answer to try to clearly present the link to a spreadsheet which provides exactly what you are requesting in your comment above and in your original question, as far as I can tell. Good luck. – David Tew Aug 18 '13 at 22:35
  • I have given edit access to the spreadsheet - apologies for that. – David Tew Aug 19 '13 at 11:41
  • script of "add column D to E" is working.I request you to post script of"add column D to E as i want add more functions to spreadsheet . and THANKS for your valuble support. – vikas bhosale Aug 20 '13 at 04:34
  • @vikasbhosale Regarding your request that I post the script of 'add column D to E': I believe that you can access all the scripts via the menu Tools>Script editor. There you will see 3 funcctions. One is the menu function, which is onOpen. You will see that within this function it references the addColumns() function, which itself is in the script, and is identical, in every way, to the one posted above in my answer to you. The third function copyColumns() is not necessary for your original question but is there to copy test values from the second sheet to the first. – David Tew Aug 21 '13 at 10:25
0

This works if you use a third column to get their sum and don't clear any columns after (as the formulas will register their values as 0 and change):

ss.getRange("F:F").setValue('=D:D+E:E');