2014-04-28 24 views
0

我想要使用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); 

} 
} 

回答

1

爲了給你的問題一個正確的答案,我想我需要知道你是如何得到source_range6_values的價值的。 一個快速的猜測是,你可能會想,因爲你想將數據寫入只有一個格使用target6.setValue代替target6.setValues ...

+0

這種工作方式,但是當它將它寫入目標工作表時,它將它寫入單獨的單元格中。我認爲問題是當我第一次執行get.Values()時插入數組中的逗號?我編輯了這個問題,以便將整個腳本包含在我要從源代碼提取單個值並將其插入到目標中的部分。單個值都可以正常工作。 –

+0

你可以嘗試的一件事: var source_range6_values = classDate.toString(); 請注意toString將返回一個字符串值,但它不會將該變量本身傳遞給一個字符串值。 – swimmingwood

1

快速&骯髒的方式將取代逗號(用空格):

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

我已經玩過GAS和變量。將它轉換爲字符串應該讓你使用字符串函數。如果這不起作用,你可以分享你的牀單的模型,讓我可以看看?

編輯:

我不得不玩它了一下,似乎谷歌的版本.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; 

所有日期是在一個小區,如果你只改變那些線(沒有錯誤)。

+0

我試過了,它出現了一個錯誤,說...... TypeError:找不到函數替換對象Mon Feb 02 2015 14:01:00 GMT-0600(CST),Tue Feb 02 2016 –

+0

編輯的答案執行無任何錯誤並刪除所有逗號。 – MickATX

+0

您應該知道javascript替換方法需要一個額外的參數來替換所有的事件:classDate.replace(/,/ g,'')這不是Google特定的腳本,只是javascript語法的正則表達式。請參閱文檔在這裏:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace –

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試圖幫助我,都提供了有價值的知識。

+0

JavaScript替換方法需要一個額外的參數來取代所有的事件:classDate.replace(/ ,/G,' ');這不是特定於Google腳本,而是針對正則表達式的JavaScript語法。請參閱文檔:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace –

0

我讀過幾個奇怪的答案在這裏... 如果你寫一個二維數組到一個工作表,它顯然會寫在多個單元格中......逗號絕對不是問題,而是對象的性質是。

只需將數組轉換成使用.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); 

} 
} 

現在,如果您想以更文明的方式格式化日期,那應該有點不同了......讓我知道你是否仍然需要它/需要它。

相關問題