How I get access to new intervals using "getNextDataRange()" of Selection Class? - javascript

I'm facing an error when I try to get values using the Method "getNextDataRange(Direction)" but I can't understand where I missed out.
first: I Opened another spreadsheet and selected the first sheet. I SET "B2" as initial current cell. Then I back to select the actual sheet to get access of "Selection" Class, so finally I can use "getNextDataRange":
Heres my code:
function ok() {
var sheets = SpreadsheetApp.openById("SHEET ID").getSheets()
var firstsheet = sheets[0].getRange("B2").activateAsCurrentCell().getSheet().getSelection().getNextDataRange(SpreadsheetApp.Direction.RIGHT).getValues
return SpreadsheetApp.getActiveSpreadsheet().getRange("B1").activate().setValue(firstsheet[0][0])
}
--
3. Then I wanna bring the values obtained in the range select by the getNextDataRange to import to my actual spreadsheet. Start from the range "B1" and taking the first line and the first column of the selection that I got previously.
The fallow error occours:
Cannot find getNextDataRange ((class)) method.
Someone has already faced that? I appreciate if someone could explain what detail I missed. I believe that is that doubt of many other too.

Related

Retrieving a row and adding it into another character sheet tab does not carry its properties

I am using Google-sheet as my database & google API request.
let rows: GoogleSpreadsheetRow = getSomeRows()
let row = rows.filter(...)
let index = (await this.memo.addRow(row)).rowIndex
addborders(this.memo, index, 0)
You can see the task are simple, Retrieve a row, store it into the variable and finaly add all the information at the end of another google sheet tab with the help of .addRow(). What I was expecting is for it to keep all its cells properties but what I got is a row added in RAW meaning there are no borders, padding etc so I resolve this by adding them manually with my small function I created called addborders.
The code is working perfectly fine but the only thing I couldn't find is how to retrieve the cell history.
If anyone knows how to do it, would be very much appreciated & maybe it could carry the formatting properties too so i don't have to use my addborders function anymore while using .addRow().
Best Regards.

Google Sheets Scripts: Can you get a range from a rangeId?

My google sheet has a cell on sheet1 that contains a link to a cell on sheet2. In my function, I am able to get the link url, but cannot figure out how to get a range from the rangeId:
var link = generatorSheet.getRange(currRow, 2)
var linkUrl = link.getRichTextValue().getLinkUrl()
Logger.log(linkUrl) // linkUrl = "rangeid=1843553975"
I've tried using getRangeByName and various other functions but keep getting a null value back, not a Range object.
Thanks in advance!
Edit: My overall goal in this is to iterate over each row in sheet1, where each cell in column 2 links to a cell in sheet2. I need to take the value from the cell in sheet2 and copy it into sheet3. In sheet1, there's a check box in column 1 of each row, so that's what I'm using to determine whether or not the linked to value will be copied. I'll have a button to kick off my function and populate sheet3, and it has to assume these links are already in place - they were done by hand prior
When you create an hyperlink to a range using the user interface, you are facing this issue. I think you may have to change the way of designing the hyperlink and try to define it by the formula
=hyperlink("#gid=123456789&range=A2","go to ...")
and then you will retrieve the range by
Logger.log(linkUrl.match(/(?<=range=).*/g))
For documentation purposes,
This is a url hash fragment:
#rangeid=1843553975
The id seems to be created, when inserting a link to a range using the user interface. This is distinctly different from a namedRange When clicked, it's appended to the url in the browser, i.e.,https://docs.google.com/spreadsheets/id/edit#rangeid=1843553975. Once appended, through onpopstate javascript event, the range linked to the id is highlighted in the browser/app.
NamedRanges has a similar workflow. It also provides a rangeid=<10 digit ID>. But, it also has a name attached to it. But even in this case, the rangeid is not retrievable, though Sheets API provides a obfuscated range id.
There was a feature request made to Google, but it was made obsolete, because of lack of response on the part of the requestor:
https://issuetracker.google.com/issues/162810351
You may create a new similar issue there with a link to this answer. Once created, link the issue here.
Other related trackers:
https://issuetracker.google.com/issues/129841094
https://issuetracker.google.com/issues/134986436

How to make google app script - calendar createEvent() to accept GSheet inputs from cells populated using array formula

A few days ago I got help from Stack Overflow to modify my then Apps Script code used to make calendar events from info on Google sheet, so as to tick a checkbox whenever an entry from the corresponding row is made and subsequently make new events only when the checkbox is unticked.
function addEvent() {
let webinarCalendar = CalendarApp.getCalendarById("blablablablablabla#gmail.com");
let calendarSheet = SpreadsheetApp.getActiveSheet();
let schedule = calendarSheet.getDataRange().getValues();
schedule.splice(0, 1);
const k = 16; // colIndex of checkbok col
const created = schedule.map(e => [e[k]]);
schedule.forEach(function(entry, i) {
if (entry[k] == true) { return; }
webinarCalendar.createEvent(entry[3], entry[14], entry[15], {description: entry[13]});
created[i][0] = true;
});
calendarSheet.getRange(2, k + 1, created.length, 1).setValues(created);
}
This current code worked just fine until 2 days ago when I updated 3 of the 4 cells with the required inputs to work on an array formula so that they get populated automatically whenever a new row entry is made.
The error on the app script console says :
Exception: The parameters (String,String,String,(class)) don't match the method signature for CalendarApp.Calendar.createEvent.
The parameters required for this createEvent() as per documentation are title(string), start time(string), finish time(string) and description(which is inside a javascript object I think and is also a string). To ensure that the datatype did not somehow get changed in the process of creating array formula, I cross checked the cells with an ISTEXT() and all of the inputs returned TRUE.
Second trial that I made was to change the splice() from (0,1) to (0,2) so that it ignores the first row which has the array formula written into the cells, which also did not fix the issue.
I would greatly appreciate if someone could show me what is causing this issue and help me fix it.
I don't know why it worked previously, but startTime and endTime should be Date.
I have checked that you columns are String.
Reference:
createEvent(title, startTime, endTime, options)
The error on the app script console says : Exception: The parameters (String,String,String,(class)) don't match the method signature for CalendarApp.Calendar.createEvent.
This is simply saying it's reading from data that is not in the proper data type. In this case, perhaps, try encasing the entries with 'new Date(entry[x])' for the respective start and end date/time entries.
For people trying to run the scripts, one underlying cause might be the fact that you may be using US locale when the date have been formatted as UK.
(e.g. Date that App Script is looking for is mm/dd/yyyy tt:tt:tt, but if you click in the formula cell it shows as dd/mm/yyyy tt:tt:tt)
What you would do is to go to Files > General > Locale > (Country of Choice) > Save settings.
You would then reload the page and try if the script is working now without that "Cannot find method createEvent(string,string,string)" error.
The line of code to use in your script would be:
SpreadsheetApp.getActive().setSpreadsheetLocale('en_UK');
You could include it in your onOpen trigger function.

Adding Variables to getRange()

Having a hard time with the below Google Scripts JS code.
Here's what it's supposed to do.
Copy and paste info from active sheet from column M to column AA
Go to sheet 2, get the last cell with data and add one row before pasting the new information. If nothing is there then paste to the top of the sheet
Upon first paste there is no active cells yet as its a fresh sheet so the info should be pasted directly at the top
This line here is giving me the trouble, I want to put in my last_row variable into getRange. Some docs say you should be able to do something like spreadsheet.getRange("A:"last_row) but that isnt working for me. Here's the current line, spreadsheet.getRange("A1").activate();
function NEW() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('M1:AA12').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true);
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
var last_row = spreadsheet.getCurrentCell().getRowIndex();
spreadsheet.getRange("A1").activate();
spreadsheet.getRange('\'MAIN SHEET\'!M1:AA12').copyTo(spreadsheet.getActiveRange(),
spreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
So essentially I am trying to paste data from one sheet to a second sheet. Upon paste I need the macro to find the last row with data in it from the previous paste and add one row so new data gets pasted below without any data overlapping.
Any ideas?
Regarding your 4th point:
This line here is giving me the trouble, I want to put in my last_row variable into getRange. Some docs say you should be able to do something like spreadsheet.getRange("A:"last_row) but that isn't working for me. Here's the current line, spreadsheet.getRange("A1").activate();
It's not working because the concatenation operator is missing:
spreadsheet.getRange("A:" + last_row)

function recalculates every time i open the sheet, I only want it to recalculate on edit

function timestamp() {
return new Date()
}
The function recalculates every-time i open the sheet. i only want it to recalculate on edit.
I have tried to mess with "onEdit" but i can never get it to work. I have also tried the "current project triggers" but that doesn't seem to fix the problem. I apologize that for the simplicity of the question, I simply can't find or figure out the answer.
Google sheets script
https://docs.google.com/spreadsheets/d/1-Ix9LUmMYNqWnz0mB2PAg9ocJ-TwszqHRbqxAchcxhc/edit#gid=0
It looks (based on your comments on the question), like you've created a custom formula and put it in a cell. If that's the case (meaning that you actually have =timestamp() in a cell), then it will always recalculate both on open and on edit.
That's just how formulas (both the standard ones and custom ones) work. You'll notice this when you have start to have too many formulas in a spreadsheet and it starts to slow down, or even not load correctly.
If you want something that only updates on edit, you'll have to write a function that does exactly that, then set a trigger that calls it on edit.
For example, this function will put the current date in cell A1 of the first sheet of the current spreadsheet:
function setTimestamp()
{
var thisSS = SpreadsheetApp.getActiveSpreadsheet();
var firstSheet = thisSS.getSheets()[0];
var firstCell = firstSheet.getRange(1, 1);
firstCell.setValue(new Date());
}
This kind of function will only run when you call it, so you can set the trigger to whatever you want. Obviously, you'll have to figure out how to select the cell where you want the date to appear.

Categories