简体   繁体   中英

How to combine two functions google-app-script?

I am new to apps script and trying to combine two functions for the same sheet, here are the functions I am using:

function cpydata() {
ImportRange(
  "1dZuOYi75xJTqQSpssmI7jQ8i6_J3I7DduoCQR9MVFM0",
  "Prospect Form!J2:T","1w5akaeADH-S9pBgCKLKltYhUntSIDUxwZkWPT0WPg_k",
  "BOB!A2"
);
};

function ImportRange(sourceID,sourceRange,destinationID,destinationRangeStart) {

const sourceSS = SpreadsheetApp.openById(sourceID);
const sourceRng = sourceSS.getRange(sourceRange);
const sourceVals = sourceRng.getValues();

console.log(sourceVals);
};

function clear() {
 var 

 sheet = SpreadsheetApp.getActive().getSheetByName('Prospect Form');
 sheet.getRange('C26:C28').clearContent()
 sheet.getRange('F3:F219').clearContent()
 sheet.getRange('G211:G219').clearContent()
 sheet.getRange('F221:F225').clearContent();
 }

It is clearing the cells, however, the copy data function is not working.What am I doing wrong?

In your comment you say you want to copy some range from a source spreadsheet (sheet 'Prospect Form') to some destination spreadsheet (sheet 'BOB') and clear the data in the source sheet. If you don't need to keep the original formatting it can be done this way:

function copy_range_without_formatting() {
  var src_id         = "1OqSv464lZ0mLsN9jApjf5Ub4MHMzBz9flw7217yYmRc";
  var src_range_str  = "Prospect Form!J2:T";
  var dest_id        = "1Kv8la3Yf8N9QB0sI-MnMc-C2zCYAKJuGiP5v_JnWlcA";
  var dest_range_str = "BOB!A2";

  // get the source range
  var src_range  = SpreadsheetApp.openById(src_id)
    .getSheetByName(src_range_str.split('!')[0]) // get the sheet 'Prospect Form'
    .getRange(src_range_str.split('!')[1]);      // get the range 'J2:T'
  
  // get values from the source range
  var values = src_range.getValues();

  // clear the source range
  src_range.clear();
  
  // copy the values to the destination sheet
  SpreadsheetApp.openById(dest_id)
    .getSheetByName(dest_range_str.split('!')[0]) // get the sheet 'BOB'
    .getRange(2,1,values.length,values[0].length) // '2,1' = 'A2'
    .setValues(values);
}

If you want to keep the original formatting it needs a more complicated solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM