I'm using the new Google Sheets API and any every time I try to get the active cell, it just returns cell A1 in the first sheet, no matter what. Here's my code:
var s = SpreadsheetApp.getActiveSpreadsheet();
var ss = s.getActiveSheet();
var result = ss.getActiveCell().getValue();
Seems like a Google bug...
This is currently working perfectly for me. If I highlight a cell in the active sheet and run your code, the value in the cell is logged for me.
You must use getActiveSheet() to specify the sheet. You cannot specify the sheet with getSheetByName()
Related
Im new to Google Sheets Scripts and I was trying to create a code to add Time to cell in an specific column using a button. I got the code down as shown below but I want the .getLastRow function to only apply to one especific column, not to find the very last row in the entire doc.
Any help?
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
function PreOpIN() {
ss.getRange(ss.getLastRow() + 1,9).setValue(new Date())
}
I am writing a simple script that basically reads a value and send it to another sheet upon clicking a button. For testing purpose I wrote:
SpreadsheetApp.flush()
var sheet = localSheet.getSheetByName('Input');
var dateTimeRange = sheet.getRange(2,2);
var dateTimeValue = dateTimeRange.getDisplayValue();
localSheet.toast(readingValue, dataTypeValue)
In my spreadsheet setting, I set:
File->Spreadsheet settings->Calculation->On change and every minute
Cell in range 2,2 has
=now()
So, every minute, the value in cell 2,2 recalculates itself.
But when I run the script, the output value that I see is always the value of the last time I modified the sheet. As long as I don't interact with the sheet, the script is unable to read the new value. I tried adding flush(), doesn't work. Anyone have any idea?
Thank you.
I think that the reason of your issue is that the sheet is not updated. In your case, unfortunately, flush() cannot be used. In order to avoid this issue, how about the following workarounds? Please think of this as just one of several answers.
In your situation, when the sheet is edited, the value retrieving by getDisplayValue() is updated. In this answer, this is used.
Workaround 1:
In this workaround, the cell "B2" is overwritten by =NOW().
Modified script:
var localSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = localSheet.getSheetByName('Input');
var dateTimeRange = sheet.getRange(2, 2);
var dateTimeValue = dateTimeRange.setFormula("=NOW()").getDisplayValue(); // Modified
localSheet.toast(dateTimeValue);
Workaround 2:
In this workaround, an empty cell is cleared. At sample script, the cell "B3" supposes the empty and this cell is not related to the main data. This cell is cleared. By this, let Spreadsheet know the sheet is edited.
Modified script:
var localSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = localSheet.getSheetByName('Input');
var dateTimeRange = sheet.getRange(2, 2);
sheet.getRange("B3").clear(); // Added
var dateTimeValue = dateTimeRange.getDisplayValue();
localSheet.toast(dateTimeValue);
Value of "B2" on the sheet "Input" is updated even if the edited cell is different from the main sheet. So you can also use localSheet.getSheetByName("Sheet1").getRange("A1").clear() instead of sheet.getRange("B3").clear().
Note:
It seems that your script might not be the latest one.
Is localSheet var localSheet = SpreadsheetApp.getActiveSpreadsheet()?
readingValue and dataTypeValue are not declared. In this answer, dateTimeValue is used.
In above workarounds, the value is updated. But the retrieved value shows the value of 4 or 5 seconds faster every time. About this, I couldn't find to do the same value with "B2". I apologize for this situation.
If this was not useful for your situation, I apologize.
I've been struggling trying to find a method to place information from a cell (a range of cells) as comments in a different cell (range of cell) in a different sheet.
Something like this, if I have Apple in Cell A1 in Sheet1 I want Apple to be inserted as a comment in Cell F1 in Sheet 2.
I tried coming up with something like this.
//I'm still working on this I have not been able to make this work//
//Ideally this will put the phone numbers as comment's in the needed cases//
var ss = SpreadsheetApp.getActiveSpreadsheet();
var targetsheet = ss.getSheetByName("report");
var sourcesheet = ss.getSheetByName("Sheet1");
var nrange = sourcesheet.getRange(2, 3, sourcesheet.getLastRow(), 1)
var sourcenotes = [nrange.getValue()]
var notes = targetsheet.getRange(2, 6, sourcesheet.getLastRow(),1)
notes.setNotes(sourcenotes);
As you can read this is not working I've tried different methods but none is working so I come to you guys for help.
The function "setNotes" takes a 2 dimension array, the value should be like this setNotes([sourcenotes]). Check the documentation.
Also if you are going to set the note for just one cell, i would recomend to use the function setNote. Here is an example on that function.
I've been busy trying to use the build-in javascript in Google Spreadsheet, however, not having worked in either javascript or Google Spreadsheet, i'm having a few difficulties.
My script is supposed to read a number (1-3) in a cell, and from that number parse an image to the cell below (I've been using the setFormula command for this).
So far it's working for 1 cell (B6 as i've choosen right now), but i would like to loop through a column with numbers in every other cell (So that after the script has run, it's number-picture-number-picture etc) - i just can't figure out how.
The code i'm using right now:
function numbtoimage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var url = 'IMAGE("https://dl.dropboxusercontent.com/s/bpqy8o796casqjl/belt.JPG?dl=0", 2)';
var url2 = 'IMAGE("https://dl.dropboxusercontent.com/s/4q8sakhkpot0h65/belt2.JPG?dl=0",2)';
var url3 = 'IMAGE("https://dl.dropboxusercontent.com/s/kvsf4z6z45rcg53/belt3.JPG?dl=0",2)';
var cell = sheet.getRange("B6")
var data = cell.getValue()
if(data==1) {cell.offset(1, 0, 1).setFormula(url);}
else if(data==2) {cell.offset(1, 0, 1).setFormula(url2);}
else if(data==3) {cell.offset(1, 0, 1).setFormula(url3);}
}
I've looked at This similar problem, but have been unable to make it work for my case.
Any help is greatly and truly appreciated!
Nicklas
You need some sort of loop to go through the data. I Would suggest a FOR loop.
Your script is currently written to get one single cell value, rather than all the values.
So it might be an idea to get all values in one go, then check whats in them.
Also from you question, it's not clear where the numbers will be found.
Only in column B?
Here is a quick example (untested), that goes through column B looking for a number and it should insert the link in the cell below based on that number. This code is based on your original example and untested but hopefully it helps.
function numbtoimage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var url = 'IMAGE("https://dl.dropboxusercontent.com/s/bpqy8o796casqjl/belt.JPG?dl=0", 2)';
var url2 = 'IMAGE("https://dl.dropboxusercontent.com/s/4q8sakhkpot0h65/belt2.JPG?dl=0",2)';
var url3 = 'IMAGE("https://dl.dropboxusercontent.com/s/kvsf4z6z45rcg53/belt3.JPG?dl=0",2)';
var values = sheet.getValues();
for(i=0; i < values.lenth ; i++){
if(values[i][1]==1) {sheet.getRange(i+2, 2).setFormula(url);}
else if(values[i][1]==2) {sheet.getRange(i+2, 2).setFormula(url2);}
else if(values[i][1]==3) {sheet.getRange(i+2, 2).setFormula(url3);}
}
}
I am trying to copy some data to the clipboard, from a Google Spreadsheet, by running a script - I want to minimize user error, this is the reason for the script. Depending on the active cell, I want the script to select 57 rows below the active cell and copy the values to the clipboard, in order to be pasted somewhere else. I cannot figure out how to do this. Here is the code that I have at the moment.
function copyDailySubsidy() {
var ss = SpreadsheetApp.getActive();
var rangeRow = ss.getActiveCell().getRow();
var rangeCol = ss.getActiveCell().getColumn();
var range = ss.getRange(rangeRow, rangeCol, 23).getValues();
//var values = ss.getRange().getValues();
Logger.log(rangeCol);
}
I can't seem to figure out how to copy the values of the range, or even select the appropriate range. Any help that you can afford would be greatly appreciated!
This is not achievable in Google Apps Script. Your script is running on a remote server, and has no access to a user's clipboard.