script to clear rows

69 views
Skip to first unread message

Picloan Limited

unread,
Jul 6, 2021, 5:09:49 PM7/6/21
to Google Apps Script Community
I have this script below that works perfectly when click button on a sheet. However, I want to clear a content in sheet name "public" with this script: data.getRange("E16:J28").clearContent()

my problem is that i dont know where to put it inside this script because i continue getting this error.

TypeError: data.getRange is not a function.
please help me







function initSettings_() {
  return [{
    bookId: '17cRhuKDDqjCRFcMKn',
    targetSheetName: 'national',
    colIndex: 12
  }, {
    bookId: '17cRhuKDDqjCRFcMKn6bs96rx1',
    targetSheetName: 'regional',
    colIndex: 13
  }, {
    bookId: '17cRhuKDDqjCRFcMKn6bs96rx1s',
    targetSheetName: 'province',
    colIndex: 14
    }, {
    bookId: '17cRhuKDDqjCRFcMKn6bs96rx1sBM6',
    targetSheetName: 'zone',
    colIndex: 15
  }, {
    bookId: '17cRhuKDDqjCRFcMKn6bs96rx1so',
    targetSheetName: 'Area',
    colIndex: 16
 }, {
    bookId: '17cRhuKDDqjCRFcMKn6',
    targetSheetName: 'parish',
    colIndex: 17

  }]
}

function run() {
  const data = SpreadsheetApp.getActive()
    .getSheetByName(' public  ').getDataRange().getValues();
  const settings = initSettings_();
  settings.forEach(setts => {
    const pData = prepareData_(data, setts.colIndex);
    if (pData && pData.length)
      SpreadsheetApp.openById(setts.bookId)
        .getSheetByName(setts.targetSheetName)
        .appendRow(pData)
        
  })
}

/**
 * @param {any[][]} array
 */
function prepareData_(array, colIndex) {
  return array.map(row => row[colIndex])
    .filter(item => item !== '')
}

Clark Lind

unread,
Jul 10, 2021, 9:48:53 AM7/10/21
to Google Apps Script Community
It looks like you have set "data" to the values inside the range, and therefore won't return the range. 

Try something like:
function run() {
  const sheet =  SpreadsheetApp.getActive().getSheetByName(' public  '); //don't get the values yet, get only the sheet
  const data = sheet.getDataRange().getValues() //store the sheet and values separately
  const settings = initSettings_();
  settings.forEach(setts => {
    const pData = prepareData_(data, setts.colIndex);
    if (pData && pData.length)
      SpreadsheetApp.openById(setts.bookId)
        .getSheetByName(setts.targetSheetName)
        .appendRow(pData)
        
  })
//Then you should be able to use
 sheet.getRange("E16:J28").clearContent();  //you have to work at the sheet level since you are likely choosing a different range than dataRange.



Reply all
Reply to author
Forward
0 new messages