How to set cursor position on a particular offset in ckeditor5 - cursor

I am trying to set cursor position in ckeditor5 to a certain offset but not able to set.
I am using following code but getting underfined errors. We are usign ckedtior5 from build. and bydefult it has selection module in it but then also it says undefined for editor.getSelection() and selection.setTo( range )
var range = editor.getSelection().getRanges()[0];
var bm = range.createBookmark();
const range = writer.createRange( start, end );
selection.setTo( range );
any idea how do it ?

Related

How to access an array of UDT <type>

In the Pine script reference manual the is a code snippet on
https://www.tradingview.com/pine-script-docs/en/v5/language/Objects.html
I want to know how to access the values of the array pivotHighArray.
This is because I don't want to draw a line only between the last pivot high and the previous pivot high. I want to draw a line/lines from the current pivot high to ANY previous pivot highs that meet certain conditions (e.g. previous pivot high must be higher than the current).
//#version=5
indicator("Pivot Points High", overlay = true)
int legsInput = input(10)
// Define the `pivotPoint` UDT containing the time and price of pivots.
type pivotPoint
int openTime
float level
// Create an empty `pivotPoint` array.
var pivotHighArray = array.new<pivotPoint>()
// Detect new pivots (`na` is returned when no pivot is found).
pivotHighPrice = ta.pivothigh(legsInput, legsInput)
// Add a new `pivotPoint` object to the end of the array for each detected pivot.
if not na(pivotHighPrice)
// A new pivot is found; create a new object of `pivotPoint` type, setting its `openTime` and `level` fields.
newPivot = pivotPoint.new(time[legsInput], pivotHighPrice)
// Add the new pivot object to the array.
array.push(pivotHighArray, newPivot)
// On the last historical bar, draw pivot labels and connecting lines.
if barstate.islastconfirmedhistory
var pivotPoint previousPoint = na
for eachPivot in pivotHighArray
// Display a label at the pivot point.
label.new(eachPivot.openTime, eachPivot.level, str.tostring(eachPivot.level, format.mintick), xloc.bar_time, textcolor = color.white)
// Create a line between pivots.
if not na(previousPoint)
// Only create a line starting at the loop's second iteration because lines connect two pivots.
line.new(previousPoint.openTime, previousPoint.level, eachPivot.openTime, eachPivot.level, xloc = xloc.bar_time)
// Save the pivot for use in the next iteration.
previousPoint := eachPivot
I have tried pivotHighArray.level[someindex], array.get(pivotHighArray.level, someindex) but failed.
How do I reference these values?
This question is related to my old question which I'm now trying another approach on since it fails when a new bar is printed both live and during backtesting
You're on the right track. Try:
pivotPoint t = na
if 0 < array.size(pivotHighArray)
t := array.get(pivotHighArray, 0)
plot(na(t) ? na : t.level, color = color.red)
First you just read the array element, after that you can access its properties.

How to extract an element of a spreadsheet as a string, not as an array element Google App Script

(Edited the problem description)
What I am trying to do: Using this Spreadsheet to collect the URLs row by row and paste each image in a new slide of a Google Slide named Output Document. I am using a function addImagetoSlide that takes in the imageUrl, the slide number and the name of the document to add images to each element of the slide.
Where I am stuck When I try to use dataRange (the array containing my spreadsheet element, it works well outside the while loop, but inside the while loop, it does not read the element [0][2] saying that "Cannot read Property 2 of line.. I do not understand why it works outside but not inside the while loop
function collateImages(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var targetDocument = SlidesApp.create("Output Document"); // Target file for the collation
var dataRange = sheet.getDataRange(); // Range of the entire database
Logger.log(dataRange.getValues()[0][2]);
Logger.log(dataRange.getValues()[1][2]);
Logger.log(dataRange.getValues()[2][2]);
Logger.log(dataRange.getValues()[3][2]);
var i = 0; // Iterator
while (i< dataRange.getLastRow()){
i = i + 1;
Logger.log(dataRange.getValues()[i][2]);
addImageToSlide(doubtRange.getValues()[i][2],i,targetDocument);
}
}
// Function to get file id from url
function getIdFromUrl(url) { return url.match(/[-\w]{25,}/); }
//Add Image with URl "ImageUrl" on slide number "index on a presentation named "deck"
function addImageToSlide(imageUrl, index,deck) {
var slide = deck.appendSlide(SlidesApp.PredefinedLayout.BLANK);
var imagefile = DriveApp.getFileById(getIdFromUrl(imageUrl));
var imageblob = imagefile.getBlob();
var image = slide.insertImage(imageblob);
}
The problem with the code you have used is that you are trying to access an index that doesn't exist. As you are incrementing the i value on the last loop, when calling the dataRange.getValues()[i][2], this will try to retrieve the element corresponding to the incremented i which falls out of the range, hence the error message you are receiving.
You can:
move the i increment after calling the dataRange.getValues()[i][2];
use a for loop;
Reference
Sheet Class Apps Script - getDataRange().

Freezing rows in Sheets with Google Apps throws type error

I'm trying to use GAS to freeze the top row of each sheet. It works, freezes the desired rows, but returns an error:
"TypeError: cannot call method setFrozenRows" of undefined (line6, file "freezeLabelRows")
According to Google documentation, the syntax is correct.
I'm running the script from the code editor attached to the sheet where I'm developing the app.
I tried a number (1) where numRowsFr is now; that was a workaround I used to dodge this error.
function rowFreeze() {
var numSheets = SpreadsheetApp.getActiveSpreadsheet().getNumSheets();
for(var i = 0; i <= numSheets; i++) {
var frSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[i];
var numRowsFr = 1;
frSheet.setFrozenRows(numRowsFr);
}
}
As I said, the code works to freeze the desired row on each sheet, but returns an error. I'd like to get the rest of this app in place to upgrade for current users.
Issue:
Array index starts at 0 and ends at length of array -1. You're looping after the end of the array(sheets array) when you use <=numSheets as the loop condition. After the last sheet, frsheet will be undefined and undefined doesn't have a setFrozenRows method as it's not a sheet type.
Solution:
Loop only till the end of the array.
Snippet:
i <= numSheets - 1;
or
i < numSheets;

Google Apps Script Replace and update cell within a range

I have a Google spreadsheet that I'm trying to remove the word "woo" within a range of cells
So far I've managed to loop through the results and log the results, however I haven't figured how to update that information in the spreadsheet itself.
Any guidance would be welcomed
Thank you
function myFunction () {
var ss = SpreadsheetApp.getActiveSheet().getRange('B:B')
var data = ss.getValues();
for (var i = 0; i < data.length; i++) {
var text = data[i].toString();
var finaltext = text.replace(/woo/g, "");
data[i] = finaltext;
Logger.log(data[i]);
}
}
Use setValues()
Notes:
Usually ss is used as a shorthand for spreadsheet, as it's used on the code for a range it's better to use range as a variable name.
setValues() returns a 2D array, so data[i] returns an array of row values rather than a cell value. To get/set cell values, use data[i][0] notation.
Considering the above replace
var ss = SpreadsheetApp.getActiveSheet().getRange('B:B')
by
var range = SpreadsheetApp.getActiveSheet().getRange('B:B')
then add the following line after the for block.
range.setValues(data);
Regarding text var declaration, replace
var text = data[i].toString();
to
var text = data[i][0].toString();
Using open ended references like B:B could lead to problems. To avoid them be sure to keep the sheet rows at minimum or better instead of using an open ended reference use something like B1:B10.

Store formatting information in an array then apply it to a range

I'm trying to create a script that will automatically format a selection based on the formatting of a table in another sheet. The idea is that a user can define a table style for header, rowOdd and rowEven in the Formats sheet, then easily apply it to a selected table using the script.
I've managed to get it working, but only by applying one type of formatting (background colour).
I based my code for reading the code into an array on this article.
As you will hopefully see from my code below, I am only able to read one formatting property into my array.
What I would like to do is read all formatting properties into the array, then apply them to the range in one go. I'm new to this so sorry if my code is a mess!
function formatTable() {
var activeRange = SpreadsheetApp.getActiveSpreadsheet().getActiveRange(); //range to apply formatting to
var arr = new Array(activeRange.getNumRows());
var tableStyleSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Formats"); //location of source styles
var tableColours = {
header: tableStyleSheet.getRange(1, 1, 1).getBackground(),
rowEven: tableStyleSheet.getRange(2, 1, 1).getBackground(),
rowOdd: tableStyleSheet.getRange(3, 1, 1).getBackground()
}
for (var x = 0; x < activeRange.getNumRows(); x++) {
arr[x] = new Array(activeRange.getNumColumns());
for (var y = 0; y < activeRange.getNumColumns(); y++) {
x == 0 ? arr[x][y] = tableColours.header :
x % 2 < 1 ? arr[x][y] = tableColours.rowOdd : arr[x][y] = tableColours.rowEven;
Logger.log(arr);
}
}
activeRange.setBackgrounds(arr);
}
Thanks!
I might be wrong but based from the list of methods given in Class Range, feature to save or store formatting details currently do not exist yet.
However, you may want to try using the following:
copyFormatToRange(gridId, column, columnEnd, row, rowEnd) or copyFormatToRange(sheet, column, columnEnd, row, rowEnd) wherein it copies the formatting of the range to the given location.
moveTo(target) wherein it cuts and paste (both format and values) from this range to the target range.
Did you know that you can get all of the different formatting elements for a range straight into an array?
E.g.
var backgrounds = sheet.getRange("A1:D50").getBackgrounds();
var fonts = sheet.getRange("A1:D50").getFontFamilies();
var fontcolors = sheet.getRange("A1:D50").getFontColors();
etc.
However, there's no way to get all of the formatting in one call unfortunately, so you have to handle each element separately. Then you can apply all of the formats in one go:
targetRng.setFontColors(fontcolors);
targetRng.setBackgrounds(backgrounds);
and so on.

Resources