首页 文章

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

提问于
浏览
0

我有一对数组(数组最多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行,文件“代码”)

编辑4/28/2014添加整个脚本:

/**
 * 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

    我在这里读了几个奇怪的答案......如果你在一张纸上写一个2D数组,它显然会被写成多个单元格...逗号绝对不是问题,但是对象的本质是 .

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

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

    并且,最后一点: .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);
    
     }
    }
    

    现在,如果你想以更文明的方式格式化日期,那应该处理得有点不同......如果你仍然需要它/想要它,请告诉我 .

  • 0

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

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

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

  • 1

    我感谢你们两位给我的帮助,试图回答这个问题 . @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试图帮助我,两者都提供了有 Value 的知识 .

相关问题