0
votes

I have an array of a couple (the array is up to 10) date/time that I want to write to a spreadsheet using getRange().setValues(). I'm converting the array to a string and it looks correct in Logger.

[Mon Feb 02 14:01:00 GMT-06:00 2015, Tue Feb 02 01:00:00 GMT-06:00 2016, , , , , , , , ]

When I try to write the string to a single cell in a sheet:

target6.setValues(source_range6_values);

I get this error:

Incorrect range width, was 10 but should be 1 (line 84, file "Code")

Edited 4/28/2014 adding entire script:

/**
 * Copies source range and pastes at first empty row on target sheet
 */
function CopyIt(){
//Establishing source and target sheets
var source_spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var target_spreadsheet = SpreadsheetApp.openById("0AhCv9Xu_eRnSdHpLTkc0d1ZURUtyTU9oRjdFbmpMUFE");

// Get source and target sheets - can be the same or different
var sourcesheet = source_spreadsheet.getSheetByName("Form Responses");
var targetsheet = target_spreadsheet.getSheetByName("Work_Orders");

//Get row of last form submission
var source_last_row = sourcesheet.getLastRow();

// Check for answer to Do you need a Flyer Created? If No, end now. If Yes, continue.
var check = sourcesheet.getRange("T"+(source_last_row)).getValue();  
if (check == 'Yes') {  

//Pulling date(s) from the users form entry (source sheet) into an array
var daterange = sourcesheet.getRange("H"+source_last_row+":Q"+source_last_row);

//Getting the values of the array
var classDate = daterange.getValues();

//changing the array values to a string
classDate.toString();

//Building a new variable with the string to be inserted below in the target sheet
var source_range6_values = classDate;

//source_range6_values.toString();
Logger.log(classDate[0]);

// Get the last row on the target sheet
var last_row = targetsheet.getLastRow();

//Setting the target cell in the Marketing Work Order sheet  
var target6 = targetsheet.getRange("U"+(last_row+1));

// Aadding a new row in the target sheet
targetsheet.insertRowAfter(last_row);

//Inserting the values of source_range6_values into the target sheet.  Unfortunately it does not enter the data into the same field and it's in military time.
target6.setValue(source_range6_values);
Logger.log(source_range6_values);

 }
}  
4

4 Answers

1
votes

To give a correct answer for your question, i guess i need to know how you get the value of source_range6_values. One quick guess is you might want to use target6.setValue instead of target6.setValues since you want to write the data into one cell only...

1
votes

A quick & dirty way would be to replace the commas(with spaces):

source = String(source_range6_values).replace("," , " ");

I've had fun with GAS and variables. Casting it as a String should let you use the string functions on it. If that doesn't work can you share a mock-up of your sheets so I can take a look?

edit:

I had to play around with it a bit, seems google's version of .replace() only replaces the first instance (and doesn't allow .replaceAll() ).

I edited your code starting on line 23:

//Getting the values of the array
var classDate = daterange.getValues().toString();    

//Building a new variable with the string to be inserted below in the target sheet
//Google has bugs, .replace() seems to only replace the first instance
//-while {} loop replaces all of them
while (!classDate.equals(classDate.replace("," , " "))) { classDate = classDate.replace("," , " "); };
var source_range6_values = classDate;

All the dates are in one cell if you change only those lines (and no errors).

0
votes

I appreciate the help you two have given me trying to answer this question. @swimmingwood fixed the actual capture of the data into a string, but it left commas and when I inserted it into the target sheet, it wrote it to multiple cells with an error. It did write to the sheet but the error had you use a CTRL-E (inside the taget sheet) to complete the insert and wrote them into separate cells.

@MickATX suggested the code to replace the commas in the string with a space, which would be fine, but apparently he discovered a Google scripting problem that would only allow for the first comma to be replaced and ignore the rest. Great knowledge never-the-less.

I ended up using a formula in an addition cell in the source sheet that looked like this:

=ArrayFormula(CONCATENATE(REPT(TEXT(H2:Q2,"mm/dd/yyyy hh:mm a")&CHAR(10),H2:Q2>0)))

This formula wrote all the date/time entries provided by the form entry into one cell of the source sheet and ONLY the number of entries (1-10). I then wrote that single cell to the target sheet via the script.

Thanks to @swimmingwood and @MickATX for trying to help me, both provided worthy knowledge.

0
votes

I've read a couple of strange answers here... If you write an 2D array to a sheet it will obviously be written accross multiple cells... commas are definitely not the issue but the nature of the object is.

Simply convert your array into a string using .toString() or .join() (the latter providing the advantage you can choose the separator to use) and setValue() (without S) at the place you want.

the commas you see in the logger are only typographic representation of array elements separators...

And, last point : the .join() or .toString() methods return new variables, they don't modify the original value so when you write classDate.toString(); you are not doing anything ... you should write it like this :

classDateAsAString = classDate.toString();

finally your code :

function CopyIt(){
//Establishing source and target sheets
var source_spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var target_spreadsheet = SpreadsheetApp.openById("0AhCv9Xu_eRnSdHpLTkc0d1ZURUtyTU9oRjdFbmpMUFE");

// Get source and target sheets - can be the same or different
var sourcesheet = source_spreadsheet.getSheetByName("Form Responses");
var targetsheet = target_spreadsheet.getSheetByName("Work_Orders");

//Get row of last form submission
var source_last_row = sourcesheet.getLastRow();

// Check for answer to Do you need a Flyer Created? If No, end now. If Yes, continue.
var check = sourcesheet.getRange("T"+(source_last_row)).getValue();  
if (check == 'Yes') {  

//Pulling date(s) from the users form entry (source sheet) into an array
var daterange = sourcesheet.getRange("H"+source_last_row+":Q"+source_last_row);

//Getting the values of the array
var classDate = daterange.getValues();


var source_range6_values = classDate.join(' & ');// using & as separator for example


// Get the last row on the target sheet
var last_row = targetsheet.getLastRow();

//Setting the target cell in the Marketing Work Order sheet  
var target6 = targetsheet.getRange("U"+(last_row+1));

// Adding a new row in the target sheet
targetsheet.insertRowAfter(last_row);

//Inserting the values of source_range6_values into the target sheet.  Unfortunately it does not enter the data into the same field and it's in military time.
target6.setValue(source_range6_values);
Logger.log(source_range6_values);

 }
}

Now if you want to format the dates in a more civilized way, that should be handled a bit differently... let me know if you still need it / want it.