I have uploaded loads of files in a gallery and I want them to show up in file name order. Sorting manually is a pain, how can I automate it?
You can run javascript code in an sjs-3 interpreter. There is a js-console for it that can access your website directly so that you don't need to create an app and run it.
Use this code snippet to sort the children of in //example.com:
var sortFilesIn = function(path) {
compare = function(a,b) {
if (a.name() < b.name()) {
return -1
} else {
return 1
}
}
parent = f.select(path)
files = []
parent.children().each(function(fi) {
files.push(fi)
})
files.sort(compare)
for(var i=0; i<files.length; i++) {
var fi = files[i]
fi.move(os.APPEND, parent, "", 0)
}
}
sortFilesIn('//example.com')
response.body('ok')
Related
I'm doing RolePlay Character Sheets on a "Parent tab" I've called "MODEL", where I masterize my formulas.
I've created a second tab "Character1" and a third one "Character2". But when I try to use =QUERY or =TEXTFORMULA or whatever. It doesn't make the formulas to calculate on the actual spreadsheet, it just get the data from the "MODEL" tab.
My only way is actually to copy/past all my formulas, but if I do a mistake, I'll have to correct it in every spreadsheet every time.
Is that possible to have a formula which take the cell at:
MODELE!AE58
And automatically generate the same formulas in every tabs:
CHARACTER1!AE58
CHARACTER2!AE58
etc...
Sorry if its blur, I'm doing my best to explain.
simple
Try
function onEdit(e) {
var sh = e.source.getActiveSheet()
var rng = e.source.getActiveRange()
if (rng.getFormula() != '' && sh.getName() == 'MODEL') {
var excl = ['MODEL', 'OTHER'];//excluded sheets
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(sh => {
if (!~excl.indexOf(sh.getSheetName())) {
sh.getRange(rng.getA1Notation()).setFormula(rng.getFormula())
}
})
}
}
when you change a formula in MODEL, this will also change in other tabs excepts excluded ones
multiple
If you edit the formulas by dragging them into the MODEL sheet, use this one which allows you to edit all the formulas at once
function onEdit(e) {
var sh = e.source.getActiveSheet()
if (sh.getName() != 'MODEL') return;
for (var i = e.range.rowStart; i <= e.range.rowEnd; i++) {
for (var j = e.range.columnStart; j <= e.range.columnEnd; j++) {
if (sh.getRange(i, j).getFormula() != '') {
var excl = ['MODEL', 'OTHER'];//excluded sheets
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(child => {
if (!~excl.indexOf(child.getSheetName())) {
child.getRange(sh.getRange(i, j).getA1Notation()).setFormula(sh.getRange(i, j).getFormula())
}
})
}
}
}
}
global
Il you need to reset all formulas, enable google sheets api and try
function onOpen() {
SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
.addItem('👉 Apply all formulas from MODEL to all tabs', 'spreadFormulas')
.addToUi();
}
function spreadFormulas() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName('MODEL')
if (sh.getName() != 'MODEL') return;
var data = [];
var formulas = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn()).getFormulas()
for (var i = 0; i < formulas.length; i++) {
for (var j = 0; j < formulas[0].length; j++) {
if (formulas[i][j] != '') {
var excl = ['MODEL', 'OTHER'];//excluded sheets
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(child => {
if (!~excl.indexOf(child.getSheetName())) {
data.push({
range: `${child.getName()}!${columnToLetter(+j + 1) + (+i + 1)}`,
values: [[`${formulas[i][j]}`]],
})
}
})
}
}
}
if (data.length) {
var resource = {
valueInputOption: 'USER_ENTERED',
data: data,
};
try { Sheets.Spreadsheets.Values.batchUpdate(resource, ss.getId()); } catch (e) { console.log(JSON.stringify(e)) }
}
}
function columnToLetter(column) {
var temp, letter = '';
while (column > 0) {
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
if your sheet is called MODELE try on some other sheet just:
=MODELE!AE58
for array it would be:
={MODELE!AE58:AE100}
also take a look into "Named Ranges" - maybe you will find it more handy
I've written a pretty simple script that successfully takes information from one sheet in a Google Spreadsheet, and replaces information in a column in another sheet in the same spreadsheet pending satisfaction of two criteria: the receiving row has the same "Customer ID" and "Product Type." I say "simple" because it's intuitive, but extremely computationally demanding (taking nearly 30 seconds to run!).
From what I've read online, it's the sequential read and write operations that are causing the slowdown. I'm assuming that if I sort the sheets in question on the two criteria and THEN do a function that writes over subsequent rows, I may be able to speed it up. I'm a little weak on algorithms, so I'm still scratching my head on how to do this elegantly.
Does anyone have any suggestions? Below is my original script, and I've already made sure that the spreadsheet collapses empty rows, so time isn't wasted iterating over nothing.
function replaceRawWithRepChanges(receivedSheet) {
var ss = SpreadsheetApp.openById(receivedSheet);
var repchanges = ss.getSheetByName('repchanges');
var rawSheet = ss.getSheetByName('Sheet1');
var rawTMtoReplace = rawSheet.getRange('P2:P');
var repCustID = repchanges.getRange('A1:A').getValues();
var repTM = repchanges.getRange('F1:F').getValues();
var repCategory = repchanges.getRange('G1:G').getValues();
var rawCustID = rawSheet.getRange('A2:A').getValues();
var rawTM = rawSheet.getRange('P2:P').getValues();
var rawCategory = rawSheet.getRange('U2:U').getValues();
var repInfo = [repCustID, repTM, repCategory];
var rawInfo = [rawCustID, rawTM, rawCategory];
for (var i=0; i < rawInfo[0].length; i++) {
for (var j=0; j < repInfo[0].length; j++) {
// var thisRawCust = rawInfo[0][i];
// var thisRepCust = repInfo[0][j];
if (rawInfo[0][i].toString() == repInfo[0][j].toString()) {
// var thisRawCategory = rawInfo[2][i];
// var thisRepCategory = repInfo[2][j];
if (rawInfo[2][i].toString() == repInfo[2][j].toString()) {
// var repvalue = repInfo[1][j];
rawInfo[1][i] = repInfo[1][j];
// var newRawValue = rawInfo[1][i];
}
}
}
}
return rawInfo[1];
}
Yes, you should sort the data (perhaps using the SORT command, which does work with multiple columns). Then, using two pointers, you only have to go down the columns once, rather than checking the entirety of repInfo for matches for every single row in rawInfo.
Once you've sorted the information, your loop might look like the following:
var i = 0;
var j = 0;
while (i < rawInfo[0].length && j < repInfo[0].length) {
if (rawInfo[0][i].toString() == repInfo[0][j].toString()) {
if (rawInfo[2][i].toString() == repInfo[2][j].toString()) {
rawInfo[1][i]=repInfo[1][j];
i++;
j++;
} else if (rawInfo[2][i].toString() < repInfo[2][j].toString()) {
i++;
} else {
j++;
}
} else if (rawInfo[0][i].toString() < repInfo[0][j].toString()) {
i++;
} else {
j++;
}
}
I have small issue with managing the downloading data of firebase to my ionic application.
For example: In the normal case [such as below code], the downloading data is normal [such as this image]
constructor(...){
this.questionsList = this.afd.list('/questions/');
}
But if I used "setInterval" [such as below code], the downloading of data increase [such as this image]
constructor(...){
this.questionsList = this.afd.list('/questions/');
this.favoritesList = this.afd.list('/favorites/',{
query:{
orderByChild:'user_id',
equalTo: userService.id,
}
})
this.joinObjects();
this.refreshIntervalId=setInterval(()=>{
this.joinObjects();
},250);
}
joinObjects(){
let TempListX=[];
this.favoritesList.take(1).subscribe(data1=>{
this.questionsList.take(1).subscribe(data2=>{
TempListX = data1.slice(0);
for(let i=0; i<data1.length; i++){
for(let j=0; j<data2.length; j++){
if(data1[i].question_id==data2[j].$key){
TempListX[i].qTitle=data2[j].title;
}
}
}
if (JSON.stringify(TempListX)===JSON.stringify(this.TempFavoritesList)) {
}else{
this.TempFavoritesList=TempListX.slice();
}
})
})
}
So is there any way to make the downloading data be such as normal case ?
As requested here is a refactored version of your code. I have to say I did not test it but it should outline the concept. The method joinObjects() is called every time an updated value/list arrives and not in a fixed interval which creates a lot of overhead. Notice the new instance variables I added and that I renamed your observables to favoritesList$ and questionsList$ (the dollar suffix is good practice to indicate that it is an observable (not a subscription, value, ...).
public questions;
public favorites;
constructor(...) {
this.questionsList$ = this.afd.list('/questions/');
this.favoritesList$ = this.afd.list('/favorites/', {
query: {
orderByChild: 'user_id',
equalTo: userService.id,
},
});
this.questionsList$.subscribe(updatedList => {
this.questions = updatedList;
this.joinObjects();
});
this.favoritesList$.subscribe(updatedList => {
this.favorites = updatedList;
this.joinObjects();
});
}
joinObjects() {
let TempListX = [];
TempListX = this.questions.slice(0);
for (let i = 0; i < this.questions.length; i++) {
for (let j = 0; j < this.favorites.length; j++) {
if (this.questions[i].question_id == this.favorites[j].$key) {
TempListX[i].qTitle = this.favorites[j].title;
}
}
}
if (JSON.stringify(TempListX) === JSON.stringify(this.TempFavoritesList)) {
} else {
this.TempFavoritesList = TempListX.slice();
}
}
I hope this brings you closer to your goal!
I just want to change some style when the CHANGE event fired.But when I change the model by insert or move a vertex or edge, the style didn't change. And the changed vertex will change it's style after I change anything again. Is anybody konws why?
Here is my code:
graph.getModel().addListener(mxEvent.CHANGE, function(sender, evt){
if(graphInited){
graph.getModel().beginUpdate();
try {
var changes = evt.getProperty('edit').changes;
for (var i = 0; i < changes.length; i++) {
var change = changes[i];
var state = graph.view.getState(change.cell);
if(state!=null){//color #1C86EE means new insert
if(state.style[mxConstants.STYLE_IMAGE_BACKGROUND]!="#1C86EE"
&& state.style[mxConstants.STYLE_STROKECOLOR]!="#1C86EE"
&& state.style[mxConstants.STYLE_FONTCOLOR]!="#1C86EE"){
graph.setCellStyles(mxConstants.STYLE_IMAGE_BACKGROUND, '#68228B', [change.cell]);
graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, '#68228B', [change.cell]);
}
}
}
} finally {
graph.getModel().endUpdate();
}
}
});
I made more recon and fond simpler solution than in my first answer.
You need to add:
evt.consume()
graph.refresh()
So final code would looks like:
graph.getModel().addListener(mxEvent.CHANGE, function(sender, evt){
if(graphInited){
graph.getModel().beginUpdate();
evt.consume();
try {
var changes = evt.getProperty('edit').changes;
for (var i = 0; i < changes.length; i++) {
var change = changes[i];
var state = graph.view.getState(change.cell);
if(state!=null){//color #1C86EE means new insert
if(state.style[mxConstants.STYLE_IMAGE_BACKGROUND]!="#1C86EE"
&& state.style[mxConstants.STYLE_STROKECOLOR]!="#1C86EE"
&& state.style[mxConstants.STYLE_FONTCOLOR]!="#1C86EE"){
graph.setCellStyles(mxConstants.STYLE_IMAGE_BACKGROUND, '#68228B', [change.cell]);
graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, '#68228B', [change.cell]);
}
}
}
} finally {
graph.getModel().endUpdate();
graph.refresh();
}
}
});
I'm working on Rhino scripts to run into Oracle Data Modeler tool and sometimes I need to get a simple output from these scripts, like a list of objects (entities, tables, etc) and some data about them.
How can I do that?
One technique that can be used is to create a Note object and use this function to update the note contents with some arbitrary text.
var print = (function() {
var notes = model.getNoteSet().toArray();
var note = null;
if (notes.length > 0) {
note = notes[0];
note.comment = "";
}
return function() {
if (note != null) {
var s = String(note.comment);
for (var i = 0; i < arguments.length; i++) {
s += arguments[i];
}
note.comment = s;
}
}
})();
You can use it this way:
print("This ", "is ", "a test", "\n");
I know I can use java API to open a text file or something like that, but update a note content seems simpler to me.