carryover problem

35 views
Skip to first unread message

gilbert_RGI

unread,
Dec 24, 2019, 12:22:40 PM12/24/19
to Google Apps Script Community
Hello experts
I encounter on the code below a problem of transfer to cell 'D5'
the dialog box takes into account line breaks \ n but the result in the cell completely ignores, how is it possible to make the expected request
Thank you very much, I hope my English is understandable.
Gilbert

I can attach an explanatory file to you if you wish


function dotest5() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName("Param");
  var sheets =    SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var sheet = sheets[0];
  // en C10 de Param il y a Marie\n \npierre\n \npaul 
  var txt1= sh.getRange("C10").getValue();
  var box = Browser.msgBox(txt1)
  Logger.log(txt1)
  sheet.getRange('D5').setValue(txt1);
}


CBM Services

unread,
Dec 24, 2019, 2:01:37 PM12/24/19
to google-apps-sc...@googlegroups.com
Bonjour Gilbert,

Your code seems to be trying to copy cell C10 from sheet named Param over to the first sheet in the spreadsheet doc.

It looks ok except you left out some semi colons to finish the last 3 lines in your code.

George

From: gilbert_RGI
Sent: ‎2019-‎12-‎24 9:22 AM
To: Google Apps Script Community
Subject: [Apps-Script] carryover problem

--
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/8aab72e6-12df-4c29-b9b0-53d71d633c54%40googlegroups.com.

gilbert_RGI

unread,
Dec 25, 2019, 3:59:21 AM12/25/19
to google-apps-sc...@googlegroups.com
@George

Thank you George
The repositioning of the semicolons does not solve the problem the copy is done well but the expected response in this copy is not that expected
I'm putting a copy of the test file for you to understand
Thank you

https://docs.google.com/spreadsheets/d/13ya9rown7Z94q3hg_Yc-jfnX6Zlv5JUi7TQSWtuhHLc/copy


you can notice that in doTest5 and 6 the msgbox takes \ n into account but not the cell

Michael Ellis

unread,
Dec 25, 2019, 8:34:40 AM12/25/19
to Google Apps Script Community
Gilbert,
I've played with a bit and made some progress, but not success yet.   Here's what I found:
The \n text inside your string must be converted to a chr(13) and then the string must be split into an array so the values may be written into different cells.  See if you can use this line of code and work from there to get the resulting data into your cells:
txt1 = txt1.split(/\\n/g);

This uses the REGEX matching string to find every occurance (globally) of the string sequence "\" followed by "n".  When that match is found, the string is "split" into different array entries.  In my results, I get an array like this:
[Marie,  , Jean,  , Paul]
From there you should be able to manipulate your code to put the results back into cell D5 (presumably D5, D6, D7, in this case).
I hope this helps.
Mike

Clark Lind

unread,
Dec 25, 2019, 9:06:40 AM12/25/19
to Google Apps Script Community
Joyeux Noël Gilbert!

I think I see what you mean. In the message box, the items are all on the same line, but everywhere else the new lines  (\n) are there. 
Try replacing this: 
var box = Browser.msgBox(txt1);

with this:
  SpreadsheetApp.getUi().alert(txt1);

That gives the new lines :)

gilbert_RGI

unread,
Dec 25, 2019, 11:02:24 AM12/25/19
to Google Apps Script Community
@ cwlind
error to typeError 

gilbert_RGI

unread,
Dec 25, 2019, 11:17:34 AM12/25/19
to google-apps-sc...@googlegroups.com
Michael Ellis
Thanks for looking into my problem
with your formula I get the result on 3 cells unfortunately this is not what I would like to obtain
i will want a line feed see two to get in a single cell
Mary
Jeans
Paul
this is what I get in the first two scripts of the file but it is not the copy of a cell on these two examples

This is the result with split 
and the formula below
sheet.getRange('c12').setValue(txt1[0]+"\n\n"+txt1[2]+"\n\n"+txt1[4]);
Thanks Michael I will try to deepen the solution.

Michael Ellis

unread,
Dec 25, 2019, 1:27:06 PM12/25/19
to Google Apps Script Community
OK, that's more clear now.   Try this - it works for me:
  txt1 = txt1.split(/\\n/g);
  txt1 = txt1.join(String.fromCharCode(10));

That was fun to figure out.   :)   Hope this helps.
Mike

gilbert_RGI

unread,
Dec 25, 2019, 3:05:14 PM12/25/19
to google-apps-sc...@googlegroups.com


Michael Ellis

I'm going to settle for the previous formula that works The one you prescribed for me recently still gives me the result Marie \ n \ nJean \ n \ nPaul


oups: after checking the split (extra space) the formula works very well


Thank you for your help

Gilbert

function dotest7() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName("Param");
  var sheets =    SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var sheet = sheets[0];
  // en C10 de Param il y a Marie\n \npierre\n \npaul 
  // in C10 of Param there is Marie \ n \ npierre \ n \ npaul
  var txt1= sh.getRange("C10").getValue();
  txt1 = txt1.split (/\\n/g);
  txt1 = txt1.join (String.fromCharCode (10));
  sheet.getRange('c13').setValue(txt1);
}


Reply all
Reply to author
Forward
0 new messages