0

Renaming part of title attachment

I need to assign the rename function to the button "Manuell Nr zuweisen".  By clicking on it, an alert field should come out in which you are asked to enter the number you want with a blank field to digit it.

With this action I should rename the first numerical part of the title of the attached file.

The field "Internet Nr."  extrapolate the number automatically with this formula:

extractx(Titel, "-[^0-9]*(\d+)[^1-9]*(\d+,?\d*)x[^1-9]*(\d+,?\d*)cm-[^1-9]+(\d+)-?(\d+)?", "", "$1 ")

 

So the value that I need to rename is "$1" and it is present in the field "Titel.

 

I used this formula to see the name of the attachment in that field:

extractx(text(Vorschau), "(.+)\/(.+)", "i", "$2")

I know that I can rename that part of the file manually with the "rename" function of the attached file. The problem is that I have colleagues who don't understand it with the risk of causing trouble

 

Thanks for all your help

19 replies

null
    • Ninox developper
    • Jacques_TUR
    • 1 yr ago
    • Reported - view

    You have two options : 

    1 - Use html in a formula :

    "set the file to rename here";
    var file := first(files(this));
    
    "set the new file name here";
    var newFileName := "test.jpg";
    
    var uniqId := "btn" + now();
    html("
    <div id='" + uniqId + "' classe='component' title='' onclick='renameFile()'
        style='width:100%;padding:0px;text-align: center; -moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;user-select: none;'>
        <div class='nx-button-text blue'>Run code</div>
    </div>
    
    <script>
        //  code to setup the button
        var me = document.getElementById('" + uniqId + "');
        me.closest('.html').style = 'padding:0px;height:34px';
        me.closest('.stringeditor').style = 'border-width: 0px;'
        var label = me.closest('.editor-html').querySelector('label')
        me.querySelector('.nx-button-text').innerText = label.innerText;
        label.style.display = 'none';
    
        // function to rename file
        function renameFile() {
            var f = util.parseNIDFile('" + file + "');
            database.renameFile(f[0] + f[1], f[2], '" + newFileName + "', (err) => { if (err) alert(err); });
        }
    </script>")

    This formula creates a button similar to those of Ninox and calls the renameFile function when you click on it.

     

    2 - Uses NativeJS

    Put this code on button formula.

    "set the file to rename here";
    var file := first(files(this));
    
    "set the new file name here";
    var newFileName := "test2.jpg";
    
    #{
        var f = util.parseNIDFile(file);
        database.renameFile(f[0]+f[1], f[2], newFileName, (err) => {if (err) alert(err);});
    }#
    
    • thomas.4
    • 1 yr ago
    • Reported - view

    Thank you Jacques TUR

    I was searching a way to give the permission to edit only the value "$1" of the title, that means only the first numeric value. I can't put a default new name for the file because it's different for each one. So eventually, Ninox has to ask me to insert the new filename with an alert and an empty field.

     

    A second possibility could be to make a button-formula that will give the permission to edit manually the field "Interne Nr" that is actually extracting the number $1 from the title.

    That means that with a button you can overwrite the extracted value with the number you will digit in the alert field

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      thomas I'm trying to understand better, you want to replace the first number in the string?

      Can this code help you?

      replacex("HAGELSCHUTZ-Nr1082-150x400cm-Backlight-nicht-hinterleuchtet-mit Schlaufe-2021", "(\d+)", "", "1234")
      

       return : HAGELSCHUTZ-Nr1234-150x400cm-Backlight-nicht-hinterleuchtet-mit Schlaufe-2021

      • thomas.4
      • 1 yr ago
      • Reported - view

      Jacques TUR Thanks for your reply. Exactly. The title of the file, however, changes every time. They have in common that it is always the first number after the "-" symbol that needs to be changed manually.. So I think it could be done through an empty field where I enter the number and the modification is done through a trigger that starts as soon as I fill in the field. So your formula helps me some but not all

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      thomas So what exactly do you need?

      • thomas.4
      • 1 yr ago
      • Reported - view

      Jacques TUR I need a formula that allows me to execute a trigger, after I entered the number in an empty field. Once entered, a trigger starts that changes the first number after the first "-" symbol in the title

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      thomas 

      Louis Cornacchia You could try it on your console :

      "//init fileName";
      var FileName := "HAGELSCHUTZ-Nr1082-150x400cm-Backlight-nicht-hinterleuchtet-mit Schlaufe-2021";
      
      "//extract number in begin of fileName";
      var Nr := extractx(FileName, "(\d+)", "", "$1");
      
      "//call JavaScript prompt to enter the new number";
      Nr := #{:text return prompt('enter the new nember', Nr ); }#;
      
      "//replace then number on fileName";
      FileName := replacex(FileName, "(\d+)", "", Nr);
      
      "//Dusplay fileName on console";
      FileName;

      It uses NativeJS and you must install it before with NinextProject : 
      NativeJS : https://forum.ninox.com/t/g9h7l3a/ninext-native-javascript
      NinextProject : https://forum.ninox.com/t/p8h78zv/ninext-project

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      thomas If you put the replacex (as below) in the Trigger After Update of a number field, it will do what you want, right?

      myFileNameField := replacex(myFileNameField, "(\d+)", "", myNumberField)
      
      • thomas.4
      • 1 yr ago
      • Reported - view

      Jacques TUR no. Unfortunately it gives me an error. I guess it could be because the content of the "title" field is generated by a formula that extracts the title from the attached file with his formula:

      extractx(text(Vorschau), "(.+)\/(.+)", "i", "$2")
      

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      thomas when you leave the mouse on the red square, an error message appears. What does it say?

    • Fred
    • 1 yr ago
    • Reported - view
    thomas said:
    Unfortunately it gives me an error. I guess it could be because the content of the "title" field is generated by a formula

     That is your problem. You can not modify a formula field by forcing a value into it.

    You have to two options:

    1) Since “title” is a formula field, You can replace the number in the number or text field that makes up the part of the name that you need to change.

    or 

    2) You can write the results to a new field.

    • thomas.4
    • 1 yr ago
    • Reported - view

    What if I'm going to edit the formula of the field where the number from the title is extracted and put there an if else function?

     

    Something like that:

     

    if 'Nr zuweisen' = null then
        extractx(Titel, "-[^0-9]*(\d+)[^1-9]*(\d+,?\d*)x[^1-9]*(\d+,?\d*)cm-[^1-9]+(\d+)-?(\d+)?", "", "$1 ")
    else 'Nr zuweisen'
    

     

     I've tried it and if there is no number in the field "Nr zuweisen" there is no problem.

    As soon as I enter a number, the internal number field remains empty. I would like it to copy the number entered in the "Nr. zuweisen" field if present and greater than 0, otherwise it should execute the extractx formula to extract the number from the title.

    I think this solution might work as workaround but maybe I'm doing something wrong in the if else formula

    • Fred
    • 1 yr ago
    • Reported - view

    You are close. Can you give me the code for the formula for the Titel field?

      • thomas.4
      • 1 yr ago
      • Reported - view

      Fred Hi! The formula is

      extractx(text(Vorschau), "(.+)\/(.+)", "i", "$2")

      To extract only the number of "Interne Nr." from the field Title, I'm using this one:

      extractx(Titel, "-[^0-9]*(\d+)[^1-9]*(\d+,?\d*)x[^1-9]*(\d+,?\d*)cm-[^1-9]+(\d+)-?(\d+)?", "", "$1 ")
      

       So I can't edit the field "Interne Nr." because it's blocked from the formula

    • Fred
    • 1 yr ago
    • Reported - view

    Looking at the starting post, you have the field 'Interne Nr.' Is that a user editable field? If not then you can try the following in your Titel formula:

    let x := if 'Nr zuweisen' = null then
        'Interne Nr.'
    else
        'Nr zuweisen'
    end;
    
    field1 + "-" + "Nr"+ x + etc...
    

    Here we create variable that checks if 'Nr zuwisen' is empty and if it is then it will use the value in 'Interne Nr.' else it will use the value in 'Nr zuwisen'.

    Then we use that variable in the formula that creates the Titel.

    • thomas.4
    • 1 yr ago
    • Reported - view

    I don't understand why this doesn't work 🤯 

    Your formula and my doesn't work

    Error on line 5 for both formula is:

    The expressions "then" and "or" provide different types of values: string, number in line 5, column 3

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      thomas This is probably because 'Nr zuweisen' is of type number and extractx of type text. Just add "number(extractx(....))" and it should work.

      'Nr zuweisen'
      • thomas.4
      • 1 yr ago
      • Reported - view

      Jacques TUR THAT WAS THE PROBLEM!! I didn't saw it 😅

      So this is the final formula that I've applied to let this work:

      if 'Nr zuweisen' = null then
          number(extractx(Titel, "-[^0-9]*(\d+)[^1-9]*(\d+,?\d*)x[^1-9]*(\d+,?\d*)cm-[^1-9]+(\d+)-?(\d+)?", "", "$1 "))
      else
          'Nr zuweisen'
      end
      

      Thank you on all of you for your help!!

    • Fred
    • 1 yr ago
    • Reported - view

    Sorry this is taking so long. Thanks for putting up with my silly questions that you have already answered in your original post.

    I think I know a solution. You have two formula fields (Titel, Interne Nr.). Which means you need a new text field if you want to change Titel with what is in Nr zuweisen.

    In the button you can try something like:

    'new text field := if 'Nr zuweisen' = null then
        Titel
    else
        replace(Titel,'Interne Nr.','Nr zuweisen')
    end
    
    

    Or you can put the formula into a new formula field (without the ‘new text field’ part) and the end user doesn’t have to press a button.

Content aside

  • Status Answered
  • 1 yr agoLast active
  • 19Replies
  • 315Views
  • 3 Following