I'm currently facing an issue while trying to paste csv data into a Google Sheet using the code provided below. Strangely, when I execute myFunction(), the data quickly appears in the Google Sheet but then vanishes instantly, almost as if sheet.clear() is being triggered unexpectedly. Interestingly, when I disable sheet.clear(), the data transference works perfectly fine. Any insights on why sheet.clear() might be misbehaving? This mystery has got me puzzled.
function myFunction(){
var spreadsheetId = 'some Id'
var csvFileId = 'some Id'
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName('Sheet1');
sheet.clear();
getCSVData(csvFileId, spreadsheetId);
}
function getCSVData(csvFileId, spreadsheetId){
var data = DriveApp.getFileById(csvFileId)
.getBlob()
.getDataAsString();
var sheetId = SpreadsheetApp.openById(spreadsheetId)
.getSheets()[0]
.getSheetId();
var resource = {
requests: [
{
pasteData: {
data: data,
coordinate: { sheetId: sheetId,
rowIndex: 0,
columnIndex: 0 },
type: 'PASTE_VALUES',
delimiter: ","
},
}
],
includeSpreadsheetInResponse: true,
};
Sheets.Spreadsheets.batchUpdate(resource, spreadsheetId);
}