0

我正在從值的電子表格中工作。我有一些代碼可以抽取一行內容進行分析。我打算用逗號分割它,但單元格內的一些字符串包含逗號(這些逗號不是經常間隔的,所以逃避它們會很困難)。我下載了一張tsv作爲tsv而不是csv並重新上傳了它,但是我嘗試分割\ t並沒有成功。 (對於很好的衡量標準,我也嘗試了\ n,\ r和\ f來查看它們是否涉及劃分單元格,他們似乎不是)。分割包含逗號(特殊字符?)的字符串

是否有特殊字符意味着「下一個單元格」或「下一個記錄」或類似的東西?我最好試着用一個特定的角色來結束每個單元格,然後在拆分後我將不得不從我的數據中去掉?我會歡迎任何其他想法!

代碼片斷:

var lastRowContents = dataSheet.getRange(lastRow, 1, 1, 21).getValues(); 
var contentChunks = lastRowContents.toString().split('\t'); 
var product = contentChunks[0]; 
Logger.log(product); 

此整行作爲一個項目輸出該數組中,像這樣:

product: Wed Jan 05 2005 02:00:00 GMT-0600 (CST),001-2005, Day-Lee Pride Beef Gyoza Potstickers, Vegetable and Beef Dumplings ,misbranded,http://www.fsis.usda.gov/wps/portal/fsis/topics/recalls-and-public-health-alerts/recall-case-archive/recall-case-archive-2005/!ut/p/a1/jZDBCoJAEIafpQdYdlZN9CgLppa7SGS2l1gW0wVTMfHQ06d0MpScOc3w_XzMYIEzLGo56EL2uqllNc3CvkMCNnEpRNz3fAiZ6acOOxDg9gjcZoBLJiBN-JFScJi5Mb9SHvzLRxsERhfTuMCilX2JdP1ocNblSlYVUvKVI9mpUg_54hIZAHt8xWKuATL2qDlbQcRM4NYvsPCHL7B-aPu8ZO9TADr0dh-fh2db/?1dmy&current=true&urile=wcm%3apath%3a%2Ffsis-archives-content%2Finternet%2Fmain%2Ftopics%2Frecalls-and-public-health-alerts%2Frecall-case-archive%2Farchives%2Fct_index271,http://www.fsis.usda.gov/wps/portal/fsis/topics/recalls-and-public-health-alerts/recall-case-archive/recall-case-archive-2005/!ut/p/a1/jZDBCoJAEIafpQdYdlZN9CgLppa7SGS2l1gW0wVTMfHQ06d0MpScOc3w_XzMYIEzLGo56EL2uqllNc3CvkMCNnEpRNz3fAiZ6acOOxDg9gjcZoBLJiBN-JFScJi5Mb9SHvzLRxsERhfTuMCilX2JdP1ocNblSlYVUvKVI9mpUg_54hIZAHt8xWKuATL2qDlbQcRM4NYvsPCHL7B-aPu8ZO9TADr0dh-fh2db/?1dmy&current=true&urile=wcm%3apath%3a%2Ffsis-archives-content%2Finternet%2Fmain%2Ftopics%2Frecalls-and-public-health-alerts%2Frecall-case-archive%2Farchives%2Fct_index386,Day-Lee Pride Beef Gyoza Potstickers, Vegetable and Beef Dumplings,Produced 10/6/2004. The products subject to recall are: One pound bags of "DAY-LEE PRIDE BEEF GYOZA POTSTICKERS, VEGETABLE AND BEEF DUMPLINGS." Each bag bears the code "28004," as well as "Est. 17309" inside the USDA mark of inspection.,The packages state that the gyozas are filled with beef, but they may instead contain shrimp, a known allergen.,The problem was discovered by the establishment.,17309 M Day-Lee Foods Inc. 13055 E. Molette St. Santa Fe Springs, CA 90670,,Approximately 2,520 pounds,California, Colorado, Georgia, Maryland, New York, and Washington.,Class I,U.S. Food and Drug Administration (FDA),,,,, 
+0

我不明白你想要做什麼...... lastRowContents已經是一個數組(二維數組,但易於轉換爲直一維數組.. ) – 2014-10-21 21:35:04

+0

對不起,如果我不清楚!我試圖將行內每個單元格的內容分配給它自己的變量,所以我需要將行分開。這有幫助嗎? – Dani 2014-10-21 21:40:25

+0

因爲lastRowContents是一個二維數組,每個單元都有lastRowContents [0] [0],lastRowContents [0] [1],lastRowContents [0] [2]等等...... – 2014-10-21 21:44:42

回答

0

(只是爲了可視性:)

因爲lastRowContents2D array (link to doc)你有每個單元格lastRowContents[0][0],lastRowContents[0][1],lastRowContents[0][2]等。

在你的代碼:

var lastRowContents = dataSheet.getRange(lastRow, 1, 1, 21).getValues(); 
var product = lastRowContents[0][0]; 
Logger.log(product); 
+0

對不起,我不確定我是否理解......我嘗試過整個行中的lastRowContents [0]和lastRowContents [0] [0],它們只是吐出第一個字母。你說的不應該是這樣的,對吧? – Dani 2014-10-21 22:19:26

+0

噸不應該...但你可以使用單一的索引它的結果是你所需要的。 :-) – 2014-10-21 22:27:00

+0

順便說一句,我沒有發明任何東西,鏈接中的文檔是非常明確的,你不覺得。? – 2014-10-21 22:28:44