0
votes

I just want a "hello world" and after an embarrassing number of hours, its still not working. I'm new to Javascript and Google App script.

Here is the simple script that fails to enter the "Hello" and I would appreciate any tips you have on getting this to work.

As you can see in the commented lines, I've tried many ways to get my "Hello" out there, but none of them work yet. The menu does work. Thanks for your help

Really what I want to do is to be able to move from cell to cell in a spread sheet, get the values there, assign it to a variable. And then to write the var value to a cell after a condition is meet in the script.

function onOpen() {
    var ss = SpreadsheetApp.getActive();
    var menuItems = [
    {name: 'Generate Coin Trades', functionName: 'GCTrades_'}
    ];
        ss.addMenu('CoinTrade', menuItems)

function GCTrades_() {
    var ss = SpreadsheetApp.getActive();
    SpreadsheetApp.setActiveSheet(ss.getSheetByName('Prices'));
    sheet.getRange("$H$1").setValue('Hello');

   //settingsSheet.activate();
   // One before the last row that has been entered in the spreadsheet
   // var LastRow = sheet.spreadsheet.getLastRow() -1;  
   // var cell1  SpreadsheetApp.getActiveSheet().getRange(LastRow,16);
   // var cell = spreadsheet.getRange("$H$1");
   // cell.setvalue('Hello')
   //var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
   // var cell = sheet.getRange(1655,16);
   // var cell = 'H1655'
   //    cell.setValue("Hello");
   //var count = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4)
   // Spreadsheet.getActiveSheet().getRange(LastRow,16).setvalue('Hello')
   // SpreadsheetApp.getActiveRange().setValue('hello')
  }    
2

2 Answers

0
votes

There are a couple of typos preventing your script from running. First, your onOpen script isn't closed.

function onOpen() {
    var ss = SpreadsheetApp.getActive();
    var menuItems = [
    {name: 'Generate Coin Trades', functionName: 'GCTrades_'}
    ];
        ss.addMenu('CoinTrade', menuItems)
} // missing closing curly brace.

You used sheet.getRange()... but didn't define sheet anywhere. You also do not need to use setActive to edit the sheet, as you can do that on a cell-by-cell basis. It is enough to define the sheet in the script.

Once your sheet is defined, you can get ranges and set the values. I kept your absolute range ($H$1) for the first cell. We then use a relative reference for the second cell by getting the position using the .getRow() and .getColumn() methods.

function GCTrades_() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('Sheet1')
    var cell1 = sheet.getRange("$H$1").setValue('Hello');
    var cell2 = sheet.getRange(cell1.getRow(), cell1.getColumn()+1).setValue('World');
}
0
votes

Try this:

function onOpen() 
{
  SpreadsheetApp.getUi().createMenu('CoinTrade')
     .addItem('Generate Coin Trades', 'GCTrades_')
     .addToUi();
}

//This is a private function so it will not show up in script editors function list. If you want it to show up on the function list then remove the trailing underscore      
function GCTrades_()
{
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Prices').activate();//The activate is not required but it will force this sheet to be on top
  sh.getRange("A1").setValue('Hello');//Do not use $s in the range here
} 

What I really want:

Really what I want to do is to be able to move from cell to cell in a spread sheet, get the values there, assign it to a variable. And then to write the var value to a cell after a condition is meet in the script.

It's not clear to me what you "really want". Perhaps, you can begin that code yourself and refine the description of what you "really want".