将多个日期/时间的字符串写入单个单元格

时间:2014-04-28 02:20:20

标签: google-apps-script google-sheets

我有一对数组(数组最多10个)日期/时间,我想用getRange()。setValues()写入电子表格。我正在将数组转换为字符串,它在Logger中看起来是正确的。

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

当我尝试将字符串写入工作表中的单个单元格时:

target6.setValues(source_range6_values);

我收到此错误:

范围宽度不正确,为10,但应为1(第84行,文件“代码”)

编辑于2014年4月28日添加整个脚本:

/**
 * 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 个答案:

答案 0 :(得分:1)

要为您的问题提供正确的答案,我想我需要知道如何获得source_range6_values的值。 一个快速的猜测是你可能想使用target6.setValue而不是target6.setValues,因为你只想将数据写入一个单元格......

答案 1 :(得分:1)

快速&脏的方法是替换逗号(带空格):

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

我对GAS和变量玩得很开心。将它作为String转换应该允许你使用它上面的字符串函数。如果这不起作用,你可以分享你的床单模型,这样我可以看看吗?

编辑:

我不得不玩一下,似乎google的.replace()版本只替换了第一个实例(并且不允许.replaceAll())。

我从第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;

如果您只更改这些行(并且没有错误),则所有日期都在一个单元格中。

答案 2 :(得分:0)

我感谢你们两位给我的帮助,试图回答这个问题。 @swimmingwood将数据的实际捕获固定为字符串,但是它留下了逗号,当我将其插入目标表时,它将其写入多个单元格并出现错误。它确实写入了工作表,但错误是你使用CTRL-E(在taget工作表内)完成插入并将它们写入单独的单元格。

@MickATX建议代码用空格替换字符串中的逗号,这很好,但显然他发现了一个Google脚本问题,它只允许替换第一个逗号并忽略其余的逗号。伟大的知识永远不会少。

我最终在源表中的附加单元格中使用了一个公式,如下所示:

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

此公式将表单条目提供的所有日期/时间条目写入源表的一个单元格中,并且只写入条目数(1-10)。然后我通过脚本将单个单元格写入目标表单。

感谢@swimmingwood和@MickATX试图帮助我,两者都提供了有价值的知识。

答案 3 :(得分:0)

我在这里读了几个奇怪的答案...... 如果你将一个2D数组写入一个工作表,它显然会被写在多个单元格中...逗号肯定不是问题,但是对象的本质是。

只需使用.toString().join()(后者提供您可以选择使用的分隔符的优势)将数组转换为字符串,并在您所在的位置使用setValue()(不含S)想。

您在记录器中看到的逗号只是数组元素分隔符的排版表示...

最后一点:.join().toString()方法返回新变量,它们不会修改原始值,因此当您编写classDate.toString();时,您没有做任何事情...... 你应该这样写:

classDateAsAString = classDate.toString();

最后你的代码:

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);

 }
}

现在,如果你想以更文明的方式格式化日期,那应该稍微处理一下......如果你仍然需要它/想要它,请告诉我。