https://code.earthengine.google.com/a1261c42c6ba4c1185c0bc98928a3482

//作者锐多宝,公众号 锐多宝的地理空间
Map.addLayer(roi)
Map.centerObject(roi,9)

//计算NDVI函数
var NDVI_pre=function (collection){
  var fun_Cloud = function(image) {
          var NDVI=image.normalizedDifference(['B8', 'B5']).rename('NDVI')
          return image.addBands(NDVI).clip(roi);
      };
  collection = ee.ImageCollection(collection)
          .map(fun_Cloud)
          ;   
  //按最大值镶嵌NDVI,并返回只有NDVI波段的图像
  var img1 = collection.qualityMosaic("NDVI")
  return img1.select("NDVI")
}
//主函数
var get_NDVI_image=function(file_name,date_start, date_end){
       
    var collection =sentinel   
                      .filterDate(date_start, date_end)//时间筛选
                      .filterBounds(roi) //地点筛选
                      .filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',34)) //云量过滤
                      .sort('CLOUD_COVER') //筛选后再按云量排序
                      
    var NDVI_IMAGE_each_month=NDVI_pre(collection)
    //加载处理结果影像·
    // Map.addLayer(collection,imageVisParam)
    Export.image.toDrive({
            image:  NDVI_IMAGE_each_month,
            description: file_name,//文件名
            folder: file_name,
            scale: 10,//分辨率
            region: roi,//区域
            maxPixels:1e13//此处值设置大一些,防止溢出
          });
        }
//哨兵2A影像是从2017年开始有全球的.获取哪一年的,写哪一年
for(var i=2021;i<2022;i++){
  //1-12月份循环
  for(var j=1;j<13;j++){
    var file_name=i+"year_"+j+"month";
    //规则化时间
    var date_start=ee.Date.fromYMD(i, j, 1)
    //时间间隔函数,做几个月的合成写几。这里我做一个月的最大值合成
    var date_end=date_start.advance(1,"month");
    //代入到NDVI的计算函数中
    get_NDVI_image(file_name,date_start,date_end)
  }
}