SetFormula

1,137 views
Skip to first unread message

Silvio Cesar Dos Santos Junior

unread,
Oct 13, 2020, 8:13:06 PM10/13/20
to Google Apps Script Community
Hi guys, what's up?

I am having problems using the function setFormula, my script adds the formulas in the right places and finds the value but it doesn't insert it in the cell and gives the error "Formula analysis error", can you guys help me? 

The REGEXREPLACE function to remove accents is working normally, but I'm not getting the PROCV (VLOOKUP) function to work as it should. I will also leave some photos of the errors if it helps.

The range with the formulas that are not working:


Proving that he finds the right value:


But it always returns this error:



This is my formula function:

function formulas(ownboardingDate){
  
  var targetSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); 
  var targetPage = targetSpreadsheet.getSheetByName("05/10");
  
  var name = targetPage.getRange("D2:D300");
  name.setFormula('=REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(C2;"[ùúüû]";"u");"[èéêë]";"e");"[àáãâäå]";"a");"[íìîï]";"i");"[óòöôõ]";"o");"ç";"c");"[ÙÚÜÛ]";"U");"[ÈÉÊË]";"E");"[ÀÁÃÂÄÅ]";"A");"[ÍÌÎÏ]";"I");"[ÓÒÖÔÕ]";"O");"Ç";"C")');
  
  var user = targetPage.getRange("E2:E30");
  user.setFormula('=PROCV(D2; Users!D2:G1840; 2; FALSO)');
  var email = targetPage.getRange("F2");
  email.setFormula('=PROCV(D2; Users!D2:G1840; 3;FALSO())');
  var passWord = targetPage.getRange("G2");
  passWord.setFormula('=PROCV(D2; Users!D2:G1840; 4;FALSO())');
  
  //style(ownboardingDate);
  
}


Message has been deleted

Silvio Cesar Dos Santos Junior

unread,
Oct 13, 2020, 8:19:29 PM10/13/20
to Google Apps Script Community
Captura de Tela 2020-10-13 às 21.17.41.png
Captura de Tela 2020-10-13 às 21.17.12.png
Captura de Tela 2020-10-13 às 21.17.25.png

Jean-Luc Vanhulst

unread,
Oct 13, 2020, 8:58:50 PM10/13/20
to google-apps-sc...@googlegroups.com
You should try to get the formula to work in a regular cell first? Right now if I paste the formula in a cell I get N/A.

Also do you need to save the original values? Otherwise you might as well replace the value instead of using a monster formula?
On top of that you'll have to make sure to use setFormulaR1C1() and use a relative value for 'C2', since I assume you want every cell in the D column to be updated and not that every cell in the D column will be individually updated?

If you simply want to update the cell values consider this code:

function strip(string) {return string.normalize("NFD").replace(/[\u0300-\u036f]/g, "") }
  var name = targetPage.getRange("D2:D300");
  var array = name.getValues();
  array.forEach( (e,indx) => array[indx][0] = strip( e[0]) ); 
  name.setValues(array);


  or if you want a formulized version:
  var name = targetPage.getRange("D2:D300");
  name.setFormula("=strip(r[-1]c[0])") // or replace strip() with a correct, working formula and use r[0]c[0] where you have C2 now.  (assuming that you want D2 to have the stripped version of C2 and D3 the stripped version of C3 etc. 

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/82840ec4-57b2-45fc-909a-91a8ed3690f2n%40googlegroups.com.

Silvio Cesar Dos Santos Junior

unread,
Oct 14, 2020, 10:04:15 AM10/14/20
to Google Apps Script Community
Yes that is exactly what i want, the formulas on E2, F2 and G2 look for the D2 value with the vlookup (procv) and the E3, F3 and G3 look for the D3 value.
I already tried to use R1C1 like you said and i had the same error.

Jean-Luc Vanhulst

unread,
Oct 14, 2020, 10:37:06 AM10/14/20
to google-apps-sc...@googlegroups.com
You have to be a little bit more specific on 'the same error'
The example formula you have doesn't work. You need to have a working formula to begin with (in a normal cell.). Then you can copy the formula and use it in your script. 

Silvio Cesar Dos Santos Junior

unread,
Oct 14, 2020, 11:18:06 AM10/14/20
to Google Apps Script Community
When i insert the exactly same formula manually on Sheets works fine, but when i use it with the setFormula method in script i have the error "Formula analysis error." even if I try to use setFormula any of these ways:

The formula: =PROCV(D2; Users!D3:G1841; 2; FALSO())

This one gives me a different error, Unknown function: "PROCV". (PROCV is VLOOKUP in portuguese) 
var user = targetPage.getRange("E2:E11");
user.setFormula('=PROCV(D2; Users!D2:G1840; 2; FALSO())') ;

This way i have the error Formula analysis error.
var user = targetPage.getRange("E2:E11");
user.setFormulaR1C1('=PROCV(R[0]C[-1]; Users!D2:G1840; 2)') ;

This way i have the error Formula analysis error.
var user = targetPage.getRange("E2:E11");
user.setFormulaR1C1('=PROCV(R[0]C[-1]; Users!D2:G1840; 2; FALSO())') ;

This way i have the error Formula analysis error.
var user = targetPage.getRange("E2:E11");
user.setFormula('=PROCV(R[0]C[-1]; Users!D2:G1840; 2; FALSO())') ;

This way i have the error Formula analysis error.
var user = targetPage.getRange("E2:E11");
user.setFormula('=PROCV(R[0]C[-1]; Users!D2:G1840; 2)') ;

Procv doc if you want to know more about:

"

Captura de Tela 2020-10-14 às 11.55.29.png
Captura de Tela 2020-10-14 às 12.03.38.png

Jean-Luc Vanhulst

unread,
Oct 14, 2020, 11:24:45 AM10/14/20
to google-apps-sc...@googlegroups.com
From what I understand you will have to use VLOOKUP in the script. The front end will still show the local name.

MANNY Carzola

unread,
Oct 14, 2020, 11:27:54 AM10/14/20
to google-apps-sc...@googlegroups.com
Hello everyone, I'm trying to upload this and make it functional in google sheets. I know how to upload to sheets do it all the time but 
the functions are not working. Need help or help for hire on this one to make this a full functional google sheet. 




--
Manny Carzola 
Former NAVY IT SPECIALIST
Bachelor Information Systems Computer Forensics
CISSP Bank Financial Security Specialist
Certified White Hat Hacker
IT HIPAA Board --- PCI Compliance
Intrusion Detection\Forensics Gov


Jean-Luc Vanhulst

unread,
Oct 14, 2020, 11:29:15 AM10/14/20
to google-apps-sc...@googlegroups.com
You;ll have to start by making it accessible to use to see! (Sharing settings)

MANNY Carzola

unread,
Oct 14, 2020, 11:33:04 AM10/14/20
to google-apps-sc...@googlegroups.com

Jean-Luc Vanhulst

unread,
Oct 14, 2020, 11:41:58 AM10/14/20
to google-apps-sc...@googlegroups.com
Yes when I click I can't open it. You need to make it at least 'anyone with the link can view'

MANNY Carzola

unread,
Oct 14, 2020, 11:49:45 AM10/14/20
to google-apps-sc...@googlegroups.com
THe sharing option is not behaving now, maybe its down . Are you able to open the excel spreadsheet on your end and test it ? Google sheets, 
the issue is the submit buttons type of function , that the info goes on the 3rd tabs

Martin Molloy

unread,
Oct 14, 2020, 12:14:41 PM10/14/20
to google-apps-sc...@googlegroups.com
i don't think the word  FALSO()  will work in your formula

I think it should just be FALSO



Silvio Cesar Dos Santos Junior

unread,
Oct 14, 2020, 12:39:55 PM10/14/20
to Google Apps Script Community
I made it work using:

var user = targetPage.getRange("E2:E1000");
 user.setFormula('=VLOOKUP(D2; Users!D2:G1840; 2; )') ;

Thanks for everyone who helped me.

Silvio Cesar Dos Santos Junior

unread,
Oct 14, 2020, 1:22:19 PM10/14/20
to Google Apps Script Community
The range is E2:E1000 because that way is more easy to delete the rows i will not use.
Reply all
Reply to author
Forward
0 new messages