我想要使用getRange()。setValues()寫入電子表格的日期/時間的數組(數組最多爲10個)。我將數組轉換爲一個字符串,它在Logger中看起來是正確的。向單個單元格寫入多個日期/時間的字符串
[週一年2月2 14:01:00 GMT-06:00 2015年,週二年2月2 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);
}
}
這種工作方式,但是當它將它寫入目標工作表時,它將它寫入單獨的單元格中。我認爲問題是當我第一次執行get.Values()時插入數組中的逗號?我編輯了這個問題,以便將整個腳本包含在我要從源代碼提取單個值並將其插入到目標中的部分。單個值都可以正常工作。 –
你可以嘗試的一件事: var source_range6_values = classDate.toString(); 請注意toString將返回一個字符串值,但它不會將該變量本身傳遞給一個字符串值。 – swimmingwood