Apps Script Utilities.parseCsv assumes new row on line breaks within double quotes

前端 未结 5 821
渐次进展
渐次进展 2020-12-18 08:39

When using Utilities.parseCsv() linebreaks encased inside double quotes are assumed to be new rows entirely. The output array from this function will have sever

5条回答
  •  既然无缘
    2020-12-18 09:35

    It may be helpful for you to use Sheets API. In my case, it works fine without replacing the CSV text that contains double-quoted multi-line text.

    First, you need to make sure of bellow:

    Enabling advanced services

    To use an advanced Google service, follow these instructions:

    1. In the script editor, select Resources > Advanced Google services....
    2. In the Advanced Google Service dialog that appears, click the on/off switch next to the service you want to use.
    3. Click OK in the dialog.

    If it is ok, you can import a CSV text data into a sheet with:

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('some_name');
    const resource = {
        requests: [
            {
                pasteData: {
                    data: csvText, // Your CSV data string
                    coordinate: {sheetId: sheet.getSheetId()},
                    delimiter: ",",
                }
            }
    
        ]
    };
    Sheets.Spreadsheets.batchUpdate(resource, ss.getId());
    

    or for TypeScript, which can be used by clasp:

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('some_name');
    const resource: GoogleAppsScript.Sheets.Schema.BatchUpdateSpreadsheetRequest = {
        requests: [
            {
                pasteData: {
                    data: csvText, // Your CSV data string
                    coordinate: {sheetId: sheet.getSheetId()},
                    delimiter: ",",
                }
            }
    
        ]
    };
    Sheets.Spreadsheets.batchUpdate(resource, ss.getId());
    

提交回复
热议问题